在由Apache POI创build的excel文件中的兼容模式

我正在使用POI创buildexcel文件(使用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中configuration不在此模式下打开文件?

尝试,

 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等检查参考。

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