读取xlsx文件时出现apache POI异常

pav*_*van 10 java exception apache-poi

我正在使用apache POI 通过添加以下依赖项来读取xlsx文件

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>

<dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.5.0</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

即使部署在tomcat中,我也会在使用jetty插件运行时遇到异常.

org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)
......

Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
...........
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xssf.model.StylesTable.readFrom(StylesTable.java:121)
    at org.apache.poi.xssf.model.StylesTable.<init>(StylesTable.java:92)
    ... 186 more
Run Code Online (Sandbox Code Playgroud)

我删除了xmlbeans依赖,因为maven将自动下载poi依赖项,仍然是同样的异常.任何帮助?

Ali*_*rkh 15

我使用版本3.12的poi.还需要以下依赖项:

compile 'org.apache.poi:ooxml-schemas:1.1'

另见http://poi.apache.org/faq.html#faq-N10025


Bru*_*der 0

我使用 3.7 但你尝试添加

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.8</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)