Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 429
Ripon Al Wasim Avatar asked Oct 15 '15 12:10

Ripon Al Wasim


1 Answers

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();
like image 126
Ripon Al Wasim Avatar answered Oct 11 '22 10:10

Ripon Al Wasim