1

我正在尝试使用 jExcel poi 在 Eclipse 中制作一个 Excel 文件。一切似乎都很好,没有错误,它甚至可以生成文件,但它们是 0b 大并且无法打开。我设法将文件从我的平板电脑上运行到我的代码的 USB 上。当我在我的 PC 上打开它时,它说:“您尝试打开的文件的格式与文件扩展名指定的格式不同.在打开之前验证文件没有损坏并且来自受信任的来源。” 然后它问我是否要打开它。当我说“是”时,它会打开一个新的空白 Excel 文件。不是我用java制作的文件。这是我的代码如下:

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    try {
        WritableWorkbook wb = createWorkbook("Busotina "+n+".xls");
            WritableSheet sheet = createSheet(wb, "Prvi",0);
                    writeCell(0, 0, "poz", true, sheet);
        writeCell(2, 0, "poz", true, sheet);
        writeCell(1, 1, "poz", true, sheet);
        writeCell(2, 3, "poz", true, sheet);

    } catch (Exception e) {
        e.printStackTrace();
    }
          }

public WritableWorkbook createWorkbook(String fileName) throws WriteException{
    WorkbookSettings wbSettings = new WorkbookSettings();
    wbSettings.setUseTemporaryFileDuringWrite(true);
    File sd = Environment.getExternalStorageDirectory();
    File dir = new File(sd.getAbsolutePath() + "/JExcelTest");
    dir.mkdirs();
    File wbfile = new File (dir, fileName);
    WritableWorkbook wb = null;
      try {
        wb=Workbook.createWorkbook(wbfile, wbSettings);
        wb.write();
        wb.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
            return wb;
}
public WritableSheet createSheet(WritableWorkbook wb, String sheetName, int sheetIndex){
    return wb.createSheet(sheetName,sheetIndex);
}
    public void writeCell(int columnPosition, int rowPosition,String contents, boolean 
                           headerCell, WritableSheet sheet) throws WriteException{
            Label newCell = new Label (columnPosition,rowPosition,contents);
            if(headerCell){
        WritableFont headerFont = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);
        WritableCellFormat headerFormat = new WritableCellFormat(headerFont);
        headerFormat.setAlignment(Alignment.CENTRE);
        newCell.setCellFormat(headerFormat);
            }
    sheet.addCell(newCell);
}

}

那么,有没有人知道为什么它制作目录,制作想要的文件,但文件大小为 0.00B,我无法打开或放入电子邮件附件或任何内容。

提前致谢 !

4

1 回答 1

1

听起来您的文件输出流未正确关闭(因此文件似乎为 0 字节,其他程序无法访问。

另一件奇怪的事情是您在进行更改之前关闭了工作簿。您应该只在插入内容后执行此操作。

您应该尝试createWorkbook像这样编写您的方法:

public WritableWorkbook createWorkbook(String fileName) throws WriteException{
    WorkbookSettings wbSettings = new WorkbookSettings();
    wbSettings.setUseTemporaryFileDuringWrite(true);
    File sd = Environment.getExternalStorageDirectory();
    File dir = new File(sd.getAbsolutePath() + "/JExcelTest");
    dir.mkdirs();
    File wbfile = new File (dir, fileName);
    WritableWorkbook wb = null;
      try {
        wb=Workbook.createWorkbook(wbfile, wbSettings);
        wb.write();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(wb != null) wb.close();
    }         
    return wb;
}
于 2012-12-02T18:51:27.943 回答