问题
I access an Excel spreadsheet using Java Apache POI (hssf
). I got the following error :
java.lang.RuntimeException: org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:143)
at org.testng.internal.Parameters.handleParameters(Parameters.java:426)
What am I missing ?
回答1:
To open an xlsx
(Office Open XML) file, you should use XSSFWorkbook instead of HSSFWorkbook, which is used for xls
(Excel 97-2003) files.
If you are using POI < 3.5, you need to upgrade to at least version 3.5, in order to be able to read xlsx
files. Here's a guide for doing the conversion, but essentially, you'd need to load the file using WorkbookFactory which takes care of the creation of either an XSSFWorkbook
or HSSFWorkbook
for you:
Workbook workbook = WorkbookFactory.create(new File("file.xlsx"))
回答2:
Make sure the excel sheet is not corrupt, by opening it. If you seen any error, save the file as MS Excel 97-2003 Worksheet.
Also make sure you have specified the filename as "**.xls"
来源:https://stackoverflow.com/questions/12494982/apache-poi-invalid-part-to-process-data