Java--Word在线预览(word转html)--强势推荐

一、新建一个maven项目

二、pom文件

<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.maven</groupId><artifactId>online-preview</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>online-preview Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><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> <!-- https://mvnrepository.com/artifact/org.apache.directory.studio/org.apache.commons.io -->
<dependency><groupId>org.apache.directory.studio</groupId><artifactId>org.apache.commons.io</artifactId><version>2.4</version>
</dependency></dependencies><build><finalName>online-preview</finalName></build>
</project>

三、Word2007ToHtml.java区分word2007和word2003

/*** */
package com.maven.controller;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;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 org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.w3c.dom.Document;/**
* <p>Title:Word2007ToHtml </p>
* <p>Company: </p>
* @author
* @date 2018年4月11日下午2:21:30
* Description:
*/
public class Word2007ToHtml {/*@Testpublic void word2007ToHtml() throws Exception {String filepath = "D:\\Tomcat8.0\\File\\";String sourceFileName =filepath+"1.docx"; String targetFileName = filepath+"1496717486420.html"; String imagePathStr = filepath+"/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(); } }} */public static void main(String[] args) throws Throwable {final String path = "C:\\Users\\Administrator\\Desktop\\My\\船员培训\\doxc\\题库\\船员用表\\";final String file = "1.doc";InputStream input = new FileInputStream(path + file);HWPFDocument wordDocument = new HWPFDocument(input);WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());wordToHtmlConverter.setPicturesManager(new PicturesManager() {public String savePicture(byte[] content, PictureType pictureType,String suggestedName, float widthInches, float heightInches) {return suggestedName;}});wordToHtmlConverter.processDocument(wordDocument);List pics = wordDocument.getPicturesTable().getAllPictures();if (pics != null) {for (int i = 0; i < pics.size(); i++) {Picture pic = (Picture) pics.get(i);try {pic.writeImageContent(new FileOutputStream(path+ pic.suggestFullFileName()));} catch (FileNotFoundException e) {e.printStackTrace();}}}Document htmlDocument = wordToHtmlConverter.getDocument();ByteArrayOutputStream outStream = new ByteArrayOutputStream();DOMSource domSource = new DOMSource(htmlDocument);StreamResult streamResult = new StreamResult(outStream);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);outStream.close();String content = new String(outStream.toByteArray());FileUtils.writeStringToFile(new File(path, "1.html"), content, "utf-8");}
}

注意:pyth:本地地址。转换完之后,又在pyth的地址中保存的html文件

不懂在下面留言即可

欢迎订阅关注公众号(JAVA和人工智能)

获取更多免费书籍、资源、视频资料

Java在线预览(word转html)--强势推荐相关推荐

  1. 使用永中文档实现java在线预览Word,Excel,Pptx,Pdf

    使用永中文档实现java在线预览Word,Excel,Pptx,Pdf 永中文档提供了在线预览的功能 永中开发者文档 如果需要直接运行,请直接修改代码中的两个参数 转换类型在下方,根据传入以及输出类型 ...

  2. 在线预览word文档

    在线预览word文档 在线预览word文档 Java 使用wps将word文件转换pdf文件 转成PDF 在线预览word(转成pdf)前端展示 欢迎来提更好的意见 在线预览word文档 昨天下午组长 ...

  3. 在线预览Word、PDF、图片(IE、Edge、Google、FireFox等主流浏览器)

    支持IE.Edge.Google.FireFox等主流浏览器处理在线预览Word.PDF.JPG.JPEG等格式文件或图片. var id=ID; if(id!=undefined){var hasI ...

  4. Springboot 超简单实现在线预览,Word文档 doc、xlsx、pdf、txt等

    前言 PDF.TXT 只要资源可访问,根本就不需要进行任何处理,直接访问查看就完事了. 也是因为这个PDF可以直接查看(现在浏览器基本支持了),那么我们实现Word文档在线预览,其实也是 把WORD文 ...

  5. (开源kkFileView、kkOffice)在线预览word、pdf、ofd、excel、ppt、压缩包、图片等等

    (开源kkFileView.kkOffice)在线预览word.pdf.ofd.excel.ppt.压缩包.图片等 前言 此项目为文件文档在线预览项目解决方案,对标业内付费产品有[永中office][ ...

  6. office在线预览 word在线预览 .net在线预览 文件在线浏览接口

    office在线预览 word在线预览 excel在线预览 文件浏览接口服务 支持移动端浏览,只要能使用浏览器上网都可以使用,不需要安装任何第三方工具. 1.word在线预览 excel在线预览,.n ...

  7. mvc直接在html页面预览pdf,Asp.net MVC 实现在线预览word、excel、ppt、pdf文件

    在线预览word.excel.ppt 原理:主要是引用第三方Dll使本地word.excel.ppt文件转换成Html 需要引用 : Aspose.Cells.dll Aspose.Slides.dl ...

  8. 前端在线预览word,excel,pdf

    前端在线预览word,excel,pdf 预览Word 预览pdf 预览Excel 预览Word 微软的在线预览功能,可以预览word.ppt.Excel.PDF 局限: 需要外网能访问文件,如果是只 ...

  9. 前端页面预览word_详解html实现在线预览word、excel、pdf等文件的功能(附代码)_WEB前端开发...

    JavaScript判断"字典"为空的方法_WEB前端开发 字典是一种存储键值对的数据结构,Javascript中的Object类内部即实现为一个字典,本文就来为大家介绍一下判断字 ...

  10. java实现在线预览word,excel,ppt文档

    项目中遇到的word文档在线预览需求,查阅很多资料决定利用openoffice转换word文档为pdf进行预览实现. 1.下载openoffice4安装 www.openoffice.org: 2.导 ...

最新文章

  1. python基础_格式化输出(%用法和format用法)(转载)
  2. Java黑皮书课后题第5章:*5.44(计算机体系结构:比特级的操作)一个short型值用16位比特存储。编写程序,提示用户输入一个short型的整数,然后显示这个整数的16比特形式,下面是运行示例
  3. C语言学习之编程实现:输入长方形的两个边长a, b和一个整数k。k=1时,输出长方形的周长 l; k=2时 ,输出长方形的面积s;当k=3时 , 输出长方形的周长1和面积s
  4. Chapter 1 First Sight——25
  5. 64位进程调用32位dll的解决方法 / 程序64位化带来的问题和思考
  6. Linux关机和重启命令总结
  7. 浪潮服务器5112面板灯_SA5112M4 – IPMI设置
  8. php mysql bool_关于mysql api中my_bool的取值范围的问题
  9. 没有借口---911谈学习
  10. Python:isinstance()和type()
  11. 蓝牙 sig base uuid_西圣XISEM真无线蓝牙耳机ASN体验:轻盈舒适,声音悦耳_
  12. XML内容要有根标签:Extra content at the end of the document
  13. 生产环境sql语句调优实战第三篇
  14. #include stdafx.h
  15. macOs 10.12.2总于修复鼠标驱动了
  16. 计算机基础常见八股问题集合(含计算机网络,操作系统,计算机组成,数据结构与算法,数据库)
  17. 洛谷题目P1425 小鱼的游泳时间
  18. xcode提交app时出现icon缺少167.png图片的问题
  19. HorizontalScrollView和ViewPager联动效果
  20. 信息系统项目管理师---第九章 项目人力资源管理历年考题

热门文章

  1. oracle数据库查表函数,Oracle数据库的常用函数列表快速查1
  2. Ubuntu 20 安装 APR
  3. 在android tegra2中添加一个camera
  4. 告别内卷,扬帆出海,小鹏汽车们找到破局最优解?
  5. 千兆宽带网接入电脑电脑却只有百兆
  6. 【Vim】No write since last change
  7. %02x与%2x 区别
  8. python计数函数:count()和value_count()
  9. p2p终结者在交换机上的机器用P2P终结者
  10. p2p与反p2p的博弈,一劳永逸真的不容易么