我使用Java Apache POI(hssf
)访问Excel电子表格。我收到以下错误:
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)
我想念什么?
最佳答案
若要open一个xlsx
(Office Open XML)文件,应使用XSSFWorkbook
而不是HSSFWorkbook
,后者用于xls
(Excel 97-2003)文件。
如果使用的POI xlsx文件。这是进行转换的guide,但是从本质上讲,您需要使用WorkbookFactory
加载文件,这会为您创建XSSFWorkbook
或HSSFWorkbook
:
Workbook workbook = WorkbookFactory.create(new File("file.xlsx"))
关于java - Apache POI-处理数据无效的部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12494982/