一、前言
最近在做Word转Html时遇到了使用POI的4.2.1版本时报错:
二、原因解析
经过对比发现4.2.1版本比旧版本多了ooxml包,导致找不到POIXMLDocumentPart,所以这里的解决方案有两个:
1.可以在项目中把旧版本的jar包源码修改后上传到maven仓库。
2.依旧使用4.2.1版本并把配套的jar包依赖一同升级。
本篇文章介绍第二种解决方法。
三、依赖包
依赖包中主要是xdocreport的版本一定要升级到2.0.2。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
四、代码实现
//将word转为html代码
File f = new File(file);
InputStream in = new FileInputStream(f);
XWPFDocument document = new XWPFDocument(in);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(document, outputStream, null);
String value = new String(outputStream.toByteArray());