1. 程式人生 > >Excel匯出列印失敗報錯 (eg HSSF instead of XSSF)

Excel匯出列印失敗報錯 (eg HSSF instead of XSSF)

錯誤資訊:

java.lang.RuntimeException: org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF) 

 

原因是因為:

InputStream inputStream = session.getServletContext().getResourceAsStream("make/xlsprint/tOUTPRODUCT.xlsx");
Workbook workbook = new XSSFWorkbook(inputStream),XSSF操作的是Excel2007以上的版本,對應檔案的字尾名是xlsx
Workbook workbook = new HSSFWorkbook(inputStream),HSSF操作的是Excel2003以前的版本,對應的檔案字尾名是xls

檢查輸入流匯入的檔案的Excel的版本,使用對應的POI操作