Apache POI异常 [英] Apache POI exception
问题描述
我需要将docx转换为PDF,而我要使用Apache POI.这是我的POM:
I need to convert a docx to a PDF and I am going with Apache POI. This is my POM:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
由于某种原因,在运行转换期间出现异常:
For some reason, I am getting an exception during when the conversion is running:
线程主"中的异常java.lang.NoClassDefFoundError:org/apache/poi/POIXMLDocumentPart位于org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.getFontsDocument(XWPFStylesDocument.java:1477)在org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.(XWPFStylesDocument.java:190)在org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.(XWPFStylesDocument.java:184)在org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.createStylesDocument(XWPFDocumentVisitor.java:166)在org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.(XWPFDocumentVisitor.java:159)在org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.(PdfMapper.java:149)在org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:55)在org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38)在org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)在temp.main.Teste(main.java:30)在temp.main.main(main.java:18)造成原因:java.lang.ClassNotFoundException:org.apache.poi.POIXMLDocumentPart位于java.net.URLClassLoader.findClass(未知源),网址为java.lang.ClassLoader.loadClass(未知源),网址为sun.misc.Launcher $ AppClassLoader.loadClass(未知源)在java.lang.ClassLoader.loadClass(未知源)...还有11个
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.getFontsDocument(XWPFStylesDocument.java:1477) at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.(XWPFStylesDocument.java:190) at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.(XWPFStylesDocument.java:184) at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.createStylesDocument(XWPFDocumentVisitor.java:166) at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.(XWPFDocumentVisitor.java:159) at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.(PdfMapper.java:149) at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:55) at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38) at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45) at temp.main.Teste(main.java:30) at temp.main.main(main.java:18) Caused by: java.lang.ClassNotFoundException: org.apache.poi.POIXMLDocumentPart at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 11 more
我用Google搜索试图找到我缺少的依赖项,至少我认为是这种情况,但是我找不到能够解决我的问题的POIXMLDocumentPart信息.
I googled trying to find what is the dependency I missing, at least I think that's the case, but I can't find information about POIXMLDocumentPart that is able to fix my issue.
这是我用来转换docx的方法:
This is the method i am using to convert the docx:
public static void Teste(File file, String destino) {
try {
InputStream doc = new FileInputStream(file);
XWPFDocument document = new XWPFDocument(doc);
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(new File(destino));
PdfConverter.getInstance().convert(document, out, options);
new File(destino);
} catch(Exception e) {
}
}
推荐答案
XDocReport是根据POI 3.17编译的.POI 4.0.0进行了一些更改,XDocReport无法与POI 4.0.0一起使用.POIXMLDocumentPart已移至包org.apache.poi.ooxml.
XDocReport is compiled against POI 3.17. POI 4.0.0 has some changes and XDocReport will not work with POI 4.0.0. POIXMLDocumentPart moved to the package org.apache.poi.ooxml.
请参见 https://github.com/opensagres/xdocreport/pull/324
更新(2019年3月):XDocReport 2.0.2已更新为使用POI 4.0.1.
Update (March 2019): Looks XDocReport 2.0.2 has been updated to use POI 4.0.1.
这篇关于Apache POI异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!