使用带有 Java 的 Apache POI 创建 excel (.xlsx) 文件后文件已损坏

问题描述:

我已经成功地使用 Apache POI API 用 Ja​​va 创建了一个 .xlsx 格式的工作簿/Excel.我的代码如下,在 D 盘中创建了一个名为RiponAlWasim.xlsx"的文件:

I have created a Workbook/Excel in .xlsx format with Java using Apache POI API successfully. My code is as below that is created a file named "RiponAlWasim.xlsx" in D drive:

Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("D:\\RiponAlWasim.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();

当我尝试打开RiponAlWasim.xlsx"时,显示文件已损坏.怎么了?

When I tried to open "RiponAlWasim.xlsx" it was shown the file is corrupted. What's the wrong?

需要至少添加一张工作表.因此,在创建工作表后,以下代码运行良好:

It needs to be added at least one sheet to the workbook. So, after creating a worksheet the following code is working well:

Workbook wb = new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("Ripon");
FileOutputStream fileOut = new FileOutputStream("D:\\RiponAlWasim.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();