使用poi将word转换为html,支持doc,docx,转换后可以保持图片、样式。
1.导入Maven包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <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>
2.转换代码
import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.WordToHtmlConverter; import org.apache.poi.xwpf.converter.core.BasicURIResolver; import org.apache.poi.xwpf.converter.core.FileImageExtractor; import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter; import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStreamWriter; public class Test { // doc转换为html void docToHtml() throws Exception { String sourceFileName = "C:\\doc\\test.doc"; String targetFileName = "C:\\html\\test.html"; String imagePathStr = "C:\\html\\image\\"; HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(sourceFileName)); 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); 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); } // docx转换为html public void docxToHtml() throws Exception { String sourceFileName = "D:\\ac\\00.docx"; String targetFileName = "D:\\ac\\test.html"; String imagePathStr = "D:\\ac\\image\\"; OutputStreamWriter outputStreamWriter = null; try { XWPFDocument document = new XWPFDocument(new FileInputStream(sourceFileName)); XHTMLOptions options = XHTMLOptions.create(); // 存放图片的文件夹 options.setExtractor(new FileImageExtractor(new File(imagePathStr))); // html中图片的路径 options.URIResolver(new BasicURIResolver("image")); outputStreamWriter = new OutputStreamWriter(new FileOutputStream(targetFileName), "utf-8"); XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance(); xhtmlConverter.convert(document, outputStreamWriter, options); } finally { if (outputStreamWriter != null) { outputStreamWriter.close(); } } }
演示地址: https://www.xiaoyun.studio/app/preview.html
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
问题内容: 我看到有一个称为的转换器,但未公开处理方法。如何传递doc文件并获取HTML文件(或)? 问题答案: 该代码现在对我有用!
问题内容: 通过使用如何将文件转换为? 我正在使用以下代码,但无法正常工作,提示错误,我想我导入了错误的类? 问题答案: 得到了解决
问题内容: 如何使用PHP将html转换为word / excel / powerpoint? 问题答案: 尝试以下PHP类: Php电子表格 PHPWord PHP演示 到目前为止,我仅使用PHPExcel,但它工作得很好并且易于学习。由于所有课程均来自同一家公司,因此我认为它们也应满足您的需求。
我需要将带有彩色单词的. docx文件转换为html。我尝试过庞大的库,但颜色丢失了。我如何才能实现我的目标?
我正在尝试使用Apache POI将文档转换为pdf,但生成的pdf文档只包含文本,它没有任何格式,如图像、表格对齐等。
问题内容: 如何使用Java将pdf文件转换为word文件? 而且,它看起来像它一样容易吗? 问题答案: 试试PDFBOX