POI insertTable()方法使用说明

一、出现问题

报错:java.lang.NoClassDefFoundError: org/openxmlformats/schemas/wordprocessingml/x2006/main/impl/CTBodyImpl$1TblListat org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTBodyImpl.getTblList(Unknown Source)at org.apache.poi.xwpf.usermodel.XWPFDocument.insertTable(XWPFDocument.java:1078)at com.stec.platform.dic.web.service.GenDicService1.getGenDic(GenDicService1.java:120)at com.stec.platform.dic.web.test.WordTest.testGenDic(WordTest.java:241)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)Caused by: java.lang.ClassNotFoundException: org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTBodyImpl$1TblListat java.net.URLClassLoader.findClass(URLClassLoader.java:381)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)… 32 more

二、问题原因


To use the new OOXML file formats, POI requires a jar containing the file format XSDs, as compiled by XMLBeans. These XSDs, once compiled into Java classes, live in the org.openxmlformats.schemas namespace.There are two jar files available, as described in the components overview section. The full jar of all of the schemas is ooxml-schemas-1.4.jar (lower versions for older releases, see table below), and it is currently around 16mb. The smaller poi-ooxml-schemas jar is only about 6mb. This latter jar file only contains the typically used parts though.Many users choose to use the smaller poi-ooxml-schemas jar to save space. However, the poi-ooxml-schemas jar only contains the XSDs and classes that are typically used, as identified by the unit tests. Every so often, you may try to use part of the file format which isn’t included in the minimal poi-ooxml-schemas jar. In this case, you should switch to the full ooxml-schemas-1.4.jar. Longer term, you may also wish to submit a new unit test which uses the extra parts of the XSDs, so that a future poi-ooxml-schemas jar will include them.There are a number of ways to get the full ooxml-schemas-1.4.jar. If you are a maven user, see the the components overview section for the artifact details to have maven download it for you. If you download the source release of POI, and/or checkout the source code from subversion, then you can run the ant task “compile-ooxml-xsds” to have the OOXML schemas downloaded and compiled for you (This will also give you the XMLBeans generated source code, in case you wish to look at this). Finally, you can download the jar by hand from the POI Maven Repository.Note that historically, different versions of ooxml-schemas were used

Version of ooxml-schemasVersion of POICommment
ooxml-schemas-1.0.jarPOI 3.5 and 3.6
ooxml-schemas-1.1.jarPOI 3.7 to POI 3.13Generics support added, can be used with POI 3.5 and POI 3.6 as well
ooxml-schemas-1.2.jarNot released
ooxml-schemas-1.3.jarPOI 3.14 and newerVisio XML format support added, can be used with POI 3.7 – POI 3.13 as well
ooxml-schemas-1.4.jarPOI 4.0.0 and newerProvide schema for AlternateContent, can be used with previous versions of POI as well

三、解决方法

去除POI依赖的精简版poi-ooxml-schemas,引入完整版ooxml-schemas(注意:如上表格所示,根据POI版本选择ooxml-schemas版本)

<dependency>
    <groupId>org.apache.poi</groupId> 
    <artifactId>poi</artifactId> 
    <exclusions> 
        <exclusion> 
            <groupId>org.apache.poi</groupId> 
            <artifactId>poi-ooxml-schemas</artifactId> 
        </exclusion> 
    </exclusions> 
    <version>3.16</version>
</dependency>
<dependency> 
    <groupId>org.apache.poi</groupId> 
    <artifactId>ooxml-schemas</artifactId> 
    <version>1.3</version>
</dependency>

四、注意事项

如果项目中使用了不同版本的POI,需要将POI及其依赖的版本设置为一致。

五、参考

http://poi.apache.org/help/faq.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注