word转HTML 最终版
来源:互联网 发布:ubuntu 修改启动界面 编辑:程序博客网 时间:2024/06/05 14:31
最终版解决word 2007版本以后,并且有文档中有表格,或者采用poi不能转换情况
项目采用maven管理,依赖为
<!-- 调用openoffice,主要用于word2pdf转换 --><dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.0.0-RELEASE</version></dependency><!-- 用于读取html --><dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.9.2</version></dependency>
具体代码如下
@Component("jodFileTypeConverter")public class JodFileTypeConverter { @Autowired OfficeDocumentConverter documentConverter; public void office2html(File officeFile, File htmlFile) { try { DocumentFormat outputFormat = loadXhtml(); documentConverter.convert(officeFile, htmlFile, outputFormat); } catch (OfficeException e) { logger.error("文件转换异常:", e); throw new RuntimeException("文件转换失败,请稍后再试"); } } private DocumentFormat loadXhtml() { DocumentFormat xhtml = new DocumentFormat("XHTML", "xhtml", "application/xhtml+xml"); xhtml.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "XHTML Writer File")); xhtml.setStoreProperties(DocumentFamily.SPREADSHEET, Collections.singletonMap("FilterName", "XHTML Calc File")); xhtml.setStoreProperties(DocumentFamily.PRESENTATION, Collections.singletonMap("FilterName", "XHTML Impress File")); xhtml.setStoreProperties(DocumentFamily.DRAWING, Collections.singletonMap("FilterName", "XHTML Draw File")); return xhtml; } /** * 得到 HTML String字符串 */ public String getHtml(){ File officeFile = ""; File htmlFile = ""; this.office2html(officeFile, htmlFile); Document htmlDoc = Jsoup.parse(htmlFile, "UTF-8"); String htmlStr = this.formatHtml(htmlDoc.outerHtml()); return htmlStr; } public String formatHtml(String con) throws IOException { ByteArrayInputStream stream = new ByteArrayInputStream(con.getBytes()); ByteArrayOutputStream tidyOutStream = new ByteArrayOutputStream(); //实例化Tidy对象 Tidy tidy = new Tidy(); //设置输入 tidy.setInputEncoding("UTF-8"); //如果是true 不输出注释,警告和错误信息 tidy.setQuiet(true); //设置输出 tidy.setOutputEncoding("UTF-8"); //不显示警告信息 tidy.setShowWarnings(false); //缩进适当的标签内容。 tidy.setIndentContent(true); //内容缩进 tidy.setSmartIndent(true); tidy.setIndentAttributes(false); //只输出body内部的内容 tidy.setPrintBodyOnly(false); //多长换行 tidy.setWraplen(1024); //输出为xhtml tidy.setXHTML(true); //类似xml输出 tidy.setXmlOut(true); //去掉没用的标签 tidy.setMakeClean(true); //去掉meta标签 tidy.setTidyMark(false); //清洗word2000的内容 tidy.setWord2000(true); //设置错误输出信息 tidy.setErrout(new PrintWriter(System.out)); tidy.parse(stream, tidyOutStream); String content = tidyOutStream.toString(); org.jsoup.nodes.Document doc = Jsoup.parse(content); if(!doc.getElementsByTag("div").isEmpty()) { doc.getElementsByTag("div").first().removeAttr("style"); } org.jsoup.nodes.Document.OutputSettings outputSettings = new org.jsoup.nodes.Document.OutputSettings(); outputSettings.syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml); doc.outputSettings(outputSettings); content=doc.html(); stream.close(); tidyOutStream.close(); return content; }}
图片采用内部图片服务器处理,此处不作说明;上传文件后,将下载的文件替换图片src属性即可(<\img src=”*.png”>)
阅读全文
0 0
- word转HTML 最终版
- 【word | html】word(doc | docx) 转 html
- java word转HTML
- Java Word转Html
- html转word
- HTML 转 Word
- poi word转html
- Java Word转Html
- java word转html
- php word 转 html
- word 转html
- HTML2DOC html转word
- php word 转 html
- Word转HTML
- word转html
- word转html
- word转html
- java html转word!
- input每四位数增加-
- sqlServer数据库导入.bak文件时出现的各种错误总结
- 云上tomcat的启动加速
- 万云+趣链 | 专注于区块链技术与接下来的发展计划
- 了解真实的『REM』手机屏幕适配
- word转HTML 最终版
- Android studio 的快捷键转换为eclipse的快捷键
- iOS经典面试题
- Java中各种集合的线程安全性!!!
- JavaScript 学习笔记 (1)
- Verify Preorder Serialization of a Binary Tree 问题及解法
- 多个表进行内连接,原理是什么?
- 欢迎使用CSDN-markdown编辑器
- 怎样选择一款性价比高的数据恢复软件