通过与Java结合使用Apache POI创建excel(.xlsx)文件后,文件已损坏 [英] File is corrupted after creating excel (.xlsx) file by using Apache POI with Java

查看:928
本文介绍了通过与Java结合使用Apache POI创建excel(.xlsx)文件后,文件已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功使用Apache POI API使用Java创建了.xlsx格式的Workbook/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();

这篇关于通过与Java结合使用Apache POI创建excel(.xlsx)文件后,文件已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆