0

I am using Apache POI 3.17 to create xlsx files and filling it with data from the db. The file gets created fine but when I try to open it, I get 'incompatible format' error even though when I inspect the file, I can see that it is a Microsoft spreadsheet file. I looked at here,here and here and tried all these examples but didn't help and I don't know where the problem is. Here is my file creation code:

File excelFile = new File("C:\\myFile.xlsx"); //a new file to be created
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(rowIndex);
Cell cell;
//some font and style creation
for(String eachHeader : headers) {
        cell = row.createCell(cellIndex);
        cell.setCellValue(eachHeader);
        cell.setCellStyle(headerStyle);
        cellIndex++;
}
//some more row and cell creation

try {
    //finally try to write all this content into the excel file
    OutputStream out = new FileOutputStream(excelFile);
    workbook.write(out);
    out.close();
    logger.debug("Worksheet created: " + excelFile.getAbsolutePath());
}catch(Exception exc) {
        logger.error("Error occured while creating or writing to the excel file: " + exc.getMessage());

}

Again, the file is created fine with some data in it as I can see that the size is not 0, but just cannot open it, why??

sticky_elbows
  • 1,344
  • 1
  • 16
  • 30
  • 1
    You should probably also close the workbook. Also, does it work if you don't write the `//some more row and cell creation` part? – XtremeBaumer Oct 09 '18 at 09:26
  • i tried closing workbook as well, and no it still doesnt work without any data, still cannot open the file – sticky_elbows Oct 09 '18 at 10:19
  • What excel version do you use? – XtremeBaumer Oct 09 '18 at 10:40
  • I use OpenOffice 4 but we tried it on different PCs with various versions of MS Excel as well @XtremeBaumer – sticky_elbows Oct 09 '18 at 10:56
  • According to [this forum post](https://forum.openoffice.org/en/forum/viewtopic.php?f=9&t=67299), you would need excel and not open office to open that file. The minimum excel version to open that kind of file is 2007 and above – XtremeBaumer Oct 09 '18 at 11:02

0 Answers0