Java使用poi将word转换为html
发布时间:2020-12-14 14:33:50 所属栏目:Java 来源:网络整理
导读:使用poi将word转换为html,支持doc,docx,转换后可以保持图片、样式。 1.导入Maven包 dependency groupIdorg.apache.poi/groupId artifactIdpoi/artifactId version3.14/version /dependency dependency groupIdorg.apache.poi/groupId artifactIdpoi-scratc
使用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:doctest.doc"; String targetFileName = "C:htmltest.html"; String imagePathStr = "C:htmlimage"; 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 0.docx"; String targetFileName = "D:actest.html"; String imagePathStr = "D:acimage"; 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 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- DOSUtil
- java – JFreechart ChartPanel没有获得Transparenent
- java – appium长按和移动元素(拖放)不起作用
- java – 在Spring中使用JSR-303注释需要做什么设置?
- java – 线程/处理程序错误 – 尚未发布指定的消息队列同步
- java – Spring MVC Web应用程序中的模板
- java – @PostConstruct为@ConversationScoped bean多次调用
- 错误:内部错误:(java.lang.NoSuchMethodError)影响我的所
- java – 当TreeModel添加新节点时,为什么我的JTree不能更新
- java 四舍五入使java保留2位小数示例讲解