java Apache POI异常
我需要将docx转换为PDF,我将使用apachepoi。这是我的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>
由于某些原因,我在转换运行期间遇到异常:
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
我在谷歌上搜索,试图找到我缺少的依赖项是什么,至少我认为是这样,但我找不到能够解决我的问题的关于POIXMLDocumentPart的信息
这是我用来转换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) {
}
}
# 1 楼答案
这两个依赖关系足以执行上述示例
# 2 楼答案
我有类似的问题,但我有“两步”脚本
1.从docx模板文档创建docx(用值替换占位符)
2.从创建的docx生成PDF
我面临的问题是两种方法使用的一些库之间的冲突。升级poi ooxml库版本后->;PDF生成器抛出
no class found java.lang.NoClassDefFoundError: org/apache/poi/POIXMLDocumentPart
,如果版本较旧,则docx生成器失败对我来说,正确配合的黄金比例是:
'组织。阿帕奇。poi',名称:'poi ooxml',版本:'3.10.1'
“神父,opensagres。xdocreport',名称:'fr.opensagres。xdocreport。转换器。docx。xwpf',版本:“1.0.5”
“神父,opensagres。xdocreport',名称:'fr.opensagres。xdocreport。核心版本:“1.0.6”
“神父,opensagres。xdocreport',名称:'组织。阿帕奇。波伊。xwpf。转换器。xhtml’,版本:“1.0.6”
希望它能帮助别人。我花了几个小时解决那个问题
# 3 楼答案
XDocReport是根据POI 3.17编译的。 POI 4.0.0有一些更改,XDocReport将无法与POI 4.0.0一起使用。 POIXMLDocumentPart已移动到包组织。阿帕奇。波伊。ooxml
见https://github.com/opensagres/xdocreport/pull/324
更新(2019年3月):XDocReport 2.0.2已更新为使用POI 4.0.1