语境:
尝试使用 Apache POI 的 poi 和 poi-ooxml 4.0.0 版本 jar 打开 XLSX 文件
问题:
程序抛出错误,如下所示。我在使用 4.0.0 版本时发现此错误,因为我能够使用 3.17 或更早版本打开 excel 文件。
希望面临的问题很清楚并等待有关如何解决此问题的意见。
注意:找到了一些相关的查询,但它们与 POI 的早期版本有关 - Link1 Link2
代码:
public static void main(String[] args) {
try {
// Open Excel XLSX file
FileInputStream fis = new FileInputStream(excelFile);
ZipSecureFile.setMinInflateRatio(0);
XSSFWorkbook workBook = new XSSFWorkbook(fis);
System.out.println(workBook);
} catch (Exception e) {
e.printStackTrace();
}
}
摇篮:
compile 'org.apache.poi:poi:4.0.0'
compile 'org.apache.poi:poi-ooxml:4.0.0'
// Tried adding the below jar's as well but dint help
// compile 'org.apache.commons:commons-collections4:4.2'
// compile 'org.apache.poi:poi-ooxml-schemas:4.0.0'
// compile 'org.apache.xmlbeans:xmlbeans:3.0.1'
错误:
org.apache.poi.ooxml.POIXMLException: org/apache/poi/POIXMLTypeLoader
at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:66)
at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:648)
at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:286)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:307)
at main.Test.main(Test.java:32)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:63)
... 5 more
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/POIXMLTypeLoader
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:86)
... 11 more
Caused by: java.lang.ClassNotFoundException: org.apache.poi.POIXMLTypeLoader
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more