引入依赖pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>wordUtil</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<revision>8.3.0</revision>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
</dependencies>
</project>
实现代码
import org.docx4j.Docx4J;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Entities;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* @author dengqianghua
* @description: docx导出html
* @date 2021/6/30 16:16
*/
public class WordUtil {
public static void main(String[] args) throws Exception {
String filePath = "/var/2.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
OutputStream os = new ByteArrayOutputStream();
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setUserBodyTail("");
htmlSettings.setUserBodyTop("");
htmlSettings.setOpcPackage(wordMLPackage);
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_NONE);
String content = os.toString();
//默认样式替换
int start = content.indexOf("<style><!--");
int end = content.indexOf("--></style>");
Map<String, String> replaceMap = getReplaceContent();
if (start > 0 && end > 0) {
replaceMap.put(content.substring(start + 7, end + 3), replaceMap.get("style"));
}
replaceMap.remove("style");
for (Map.Entry<String, String> item : replaceMap.entrySet()) {
content = content.replace(item.getKey(), item.getValue());
}
new FileOutputStream("/var/1.html").write(htmlToXhtml(content).getBytes(StandardCharsets.UTF_8));
}
/**
* html转xhtml不然导出会存在问题
* @param html
* @return
*/
public static String htmlToXhtml(String html) {
org.jsoup.nodes.Document doc = Jsoup.parse(html);
doc.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);
return doc.html();
}
/**
* 替换的内容
* @return
*/
static Map<String, String> getReplaceContent() {
Map<String, String> replaceMap = new HashMap<>();
replaceMap.put("style", "html, body, div, span, h1, h2, h3, h4, h5, h6, p, a, img, table, caption, tbody, tfoot, thead, tr, th, td " +
"{ margin: 0; padding: 0; border: 0;}" +
"body {line-height: 1.5;} ");
replaceMap.put("TO HIDE THESE MESSAGES, TURN OFF debug level logging for org.docx4j.convert.out.common.writer.AbstractMessageWriter", "");
replaceMap.put("<script type=\"text/javascript\"><!--function toggleDiv(divid){if(document.getElementById(divid).style.display == 'none'){document.getElementById(divid).style.display = 'block';}else{document.getElementById(divid).style.display = 'none';}}\n" +
"--></script>", "");
return replaceMap;
}
}