Apache POI创建时excel文件中的兼容模式

时间:2014-06-11 10:03:05

标签: java excel apache-poi

我正在使用POI创建excel文件(使用HSSFWorkbook)并使用xls文件保存。但当我使用更高职位(2007+)时,我在标题栏中收到警告:Filename.xls [兼容模式]。

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");

Row row = sheet.createRow((short) 0);
Cell cell = row.createCell(0);
cell.setCellValue(1);

FileOutputStream fileOut = new FileOutputStream("D:/testExcel/workbook.xls");
wb.write(fileOut);
fileOut.close();

如何在POI中配置以不在此模式下打开文件?

1 个答案:

答案 0 :(得分:0)

尝试,

Workbook wb = WorkbookFactory.create(myExcelFile);
if (wb instanceof HSSFWorkbook) {
    // do whatever
} else if (wb instanceof SXSSFWorkbook) {
    // do whatever
} else if (wb instanceof XSSFWorkbook) {
    // do whatever
}

您的myExcelFile可以是InputStreamFile等。请检查reference.

这将允许您处理.xls和.xlsx扩展文件。也许你的问题也可以解决。