源码地址:https://github.com/sunmaolin/wordToHtml
含doc转html,docx转html等相关实例代码。一看就会,一写就对。
前言
提示:本文使用POI工具进行转换。
需求:公司之前发布的报告内容是word文件。现在需要改为富文本编辑嵌入到页面中(为了被百度检索到)。之前发布的报告也需要修改保存。那现在出现的问题就是word如何转换为html格式,且图片需要上传到云存储中。
问题1: poi只针对于doc格式的文件可以做转换。docx做不到。
问题2: 针对问题1,我将docx另存为doc文件。转换后发现样式是写在style标签中。而我需要内联样式。也就是转为html文件是可以的,但是转为html片段可行性不高。当然也可以使用jsoup进行解析处理(太费劲)。
问题3: 针对问题2,我检索发现存在xdocreport插件可以将poi中的XWPF转换为HTML,且可以为内联样式。但是其中图片是存储到本地。我需要上传到云存储中。
针对问题3,下面是解决方案。
一、xdocreport是什么?
XDocReport means XML Document reporting. It’s Java API to merge XML document created with MS Office (docx) or OpenOffice (odt), LibreOffice (odt) with a Java model to generate report and convert it if you need to another format (PDF, XHTML…).
github:XDocReport
二、POM文件
新旧版本的使用方式不一样。但是原理是一模一样。由于XDocReport中已经依赖了POI。所以无需额外引入。如果项目中存在POI依赖。注意版本冲突。
2.1 老版本
<!--github https://github.com/opensagres/xdocreport -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.5</version>
</dependency>
2.2 新版本
新版本,将名字改了,不在以org.apache.poi命名。
<!--github https://github.com/opensagres/xdocreport -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId>
<version>2.0.3</version>
</dependency>
三、编码
此处只以新版本2.0.3进行编码。推荐使用。
3.1 自定义ImageManager
import fr.opensagres.poi.xwpf.converter.core.ImageManager;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 自定义word转html图片处理器
* @author 孙茂林
* @date 2022-06-10
*/
public class CustomImageManager extends ImageManager {
/**
* 根据插件自动生成的地址与上传到服务器的地址做映射缓存
*/
Map<String, String> imageUrlCache = new HashMap<>();
public CustomImageManager() {
super(null, null);
}
@Override
public String resolve(String uri) {
return imageUrlCache.get(uri);
}
@Override
public void extract(String imagePath, byte[] imageData) throws IOException {
/*参数imagePath example: word/media/image1.png word/media/image2.png */
// 1.保存图片到云存储服务器返回访问链接
String serverImageUrl = "https://aliyun-xxx.fervwswv.image";
// 2.做映射缓存
imageUrlCache.put(imagePath, serverImageUrl);
}
}
3.2.转换为html
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* @author 孙茂林
* @date 2022-06-10
*/
public class CustomTestHtml {
private static final String sourceFile = "C:\\Users\\QM\\Desktop\\a.docx";
private static final String targetFile = "C:\\Users\\QM\\Desktop\\a.html";
@Test
public void poiDocxNew() throws Exception {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
XWPFDocument wordDocument = new XWPFDocument(fileInputStream);
// 使用自定义图片管理。直接将文件上传至云服务器
XHTMLOptions xhtmlOptions = XHTMLOptions.create().setImageManager(new CustomImageManager());
// 将样式都写为内联样式,而不是写到style标签中 默认false
xhtmlOptions.setFragment(true);
// 省略掉footer,header标签 默认false
xhtmlOptions.setOmitHeaderFooterPages(true);
// 忽略未用到的样式 默认true
xhtmlOptions.setIgnoreStylesIfUnused(true);
// 缩进 默认不缩进
// xhtmlOptions.indent(10);
XHTMLConverter.getInstance().convert(wordDocument, new FileOutputStream(targetFile), xhtmlOptions);
}
}