Java Apache POI错误:找不到XMLEventFactory提供程序com.ctc.wstx.stax.WstxEventFactory

时间:2018-01-23 14:20:45

标签: java xml apache maven apache-poi

我收到了运行时错误:

javax.xml.stream.XMLEventFactory: Provider com.ctc.wstx.stax.WstxEventFactory not found

使用Apache POI并尝试创建XSSFWorkbook

我可以毫无问题地创建HSSFWorkbook,但是当我将代码更改为:

Workbook workbook = new XSSFWorkbook();

导致运行时错误。什么想法可能是错的?以下是我为Apache POI添加的Maven依赖项:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.3</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
</dependency>

2 个答案:

答案 0 :(得分:0)

谢谢,@ Fracis @yoav!通过您的提示,我可以通过将我的pom.xml依赖项重新定义为以下内容来修复错误:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>com.ctc.wstx</groupId>
    <artifactId>woodstox-osgi</artifactId>
    <version>3.2.1.1</version>
</dependency>

再次感谢!

答案 1 :(得分:0)

对我来说,解决此问题的原因是具有以下依赖性:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
    <scope>provided</scope>
</dependency>

<!-- BE AWARE! It is absolutely crucial for wstx-asl to be bundled with plugin although it is present in Jira as well. -->
<!-- Jira version is not exported and it is a known bug: https://jira.atlassian.com/browse/JRASERVER-26214 -->
<!-- This artifact is supplemental for Apache POI library. -->
<dependency>
    <groupId>org.codehaus.woodstox</groupId>
    <artifactId>wstx-asl</artifactId>
    <version>3.2.4</version>
</dependency>

并在不同的上下文中运行XSSFWorkbook的创建: XSSFWorkbook

 xssfWorkbook;
try {
    xssfWorkbook = ContextClassLoaderSwitchingUtil.runInContext(WstxEventFactory.class.getClassLoader(), (Callable<XSSFWorkbook>) XSSFWorkbook::new);
} catch (Exception e) {
    throw new IllegalStateException("Cannot create XLSX workbook with WstxEventFactory class loader.");
}