File damaged after creating excel file (.xlsx) using Apache POI with Java

I created a Workbook / Excel in .xlsx format using Java using the Apache POI API. My code is as follows: a file called "RiponAlWasim.xlsx" is created in the D file:

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", the file was shown to be corrupt. What's wrong?

+4
source share
1 answer

At least one sheet must be added to the book. So, after creating the worksheet, the following code works well:

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

Source: https://habr.com/ru/post/1611782/


All Articles