项目后端使用maven,前端使用了富文本编辑器。目前从html转换的word为doc格式,也能将图片进行处理。网上的一些例子好多都是图片无法解析,这个地方千万要注意对图片的路径进行解析,src必须是全路径 域名+图片的正式路径,如果您的需求对图片的大小有限制的话也要修改img的style属性,如果修改完没有效果将style去掉直接加上宽和高。
一.添加maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
二.word转换为html
public static String docToHtml() throws Exception {
File path = new File(ResourceUtils.getURL(“classpath:”).getPath());
String imagePathStr = path.getAbsolutePath() + “\static\image\”;
String sourceFileName = path.getAbsolutePath() + “\static\test.doc”;
String targetFileName = path.getAbsolutePath() + “\static\test2.html”;
File file = new File(imagePathStr);
if(!file.exists()) {
file.mkdirs();
}
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(sourceFileName));
org.w3c.dom.Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document);
//保存图片,并返回图片的相对路径
wordToHtmlConverter.setPicturesManager((content, pictureType, name, width, height) -> {
try (FileOutputStream out = new FileOutputStream(imagePathStr + name)) {
out.write(content);
} catch (Exception e) {
e.printStackTrace();
}
return “image/” + name;
});
wordToHtmlConverter.processDocument(wordDocument);
org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(new File(targetFileName));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, “utf-8”);
serializer.setOutputProperty(OutputKeys.INDENT, “yes”);
serializer.setOutputProperty(OutputKeys.METHOD, “html”);
serializer.transform(domSource, streamResult);
return targetFileName;
docx格式转换为html
public static String docxToHtml() throws Exception {
File path = new File(ResourceUtils.getURL(“classpath:”).getPath());
String imagePath = path.getAbsolutePath() + “\static\image”;
String sourceFileName = path.getAbsolutePath() + “\static\test.docx”;
String targetFileName = path.getAbsolutePath() + “\static\test.html”;
OutputStreamWriter outputStreamWriter = null;
try {
XWPFDocument document = new XWPFDocument(new FileInputStream(sourceFileName));
XHTMLOptions options = XHTMLOptions.create();
// 存放图片的文件夹
options.setExtractor(new FileImageExtractor(new File(imagePath)));
// html中图片的路径
options.URIResolver(new BasicURIResolver("image"));
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(targetFileName), "utf-8");
XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getI