Android要优秀。 jExecl制作0b文件

我正在尝试使用jExcel poi在Eclipse中创build一个excell文件。 一切看起来不错,没有错误,它甚至使文件,但他们是0B大,无法打开。 我设法从我的平板电脑,我的平板电脑上运行我的代码的文件。当我打开它在我的电脑上它说:“您试图打开的文件格式不同于文件扩展名指定的格式Veify文件没有被破解,并且在打开之前来自可信源。“ 然后问我是否想打开它。 当我说'是'时,它会打开一个新的BLANK excell文件。 不是我在java中创build的文件。 这里是我的代码如下:

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大,我不能打开或附加电子邮件或任何东西。

提前致谢 !

这听起来像你的文件输出stream没有正确closures(因此,文件似乎是0字节,不能被其他程序访问。

另一件奇怪的事情是,在进行更改之前closures工作簿。 只有在插入内容后才能做到这一点。

你应该尝试像这样编写你的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; }