我正在使用Apache POI 3.17
创建xlsx
文件,并用数据库中的数据填充它。该文件可以很好地创建,但是当我尝试打开该文件时,即使检查该文件,也会看到“不兼容的格式”错误,我可以看到它是一个Microsoft电子表格文件。我查看了here,here和here并尝试了所有这些示例,但没有帮助,我也不知道问题出在哪里。这是我的文件创建代码:
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());
}
再次,该文件创建良好,其中包含一些数据,因为我可以看到大小不为0,但无法打开它,为什么?