Skip to content
Advertisement

File is corrupted after creating excel (.xlsx) file by using Apache POI with Java

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();

When I tried to open “RiponAlWasim.xlsx” it was shown the file is corrupted. What’s the wrong?

Advertisement

Answer

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();
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement