文档转成html在线预览,java poi Word文档转为HTML文件 实现在线预览功能
Java代码
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
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.PictureType;
import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
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;
/**
* word 转换成html 2017-2-27
*/
public class Word2Html {
/**
* 将word2003转换为html文件 2017-2-27
*
* @param wordPath
* word文件路径
* @param wordName
* word文件名称无后缀
* @param suffix
* word文件后缀
* @throws IOException
* @throws TransformerException
* @throws ParserConfigurationException
*/
public static String Word2003ToHtml(String wordPath, String wordName,
String suffix) throws IOException, TransformerException,
ParserConfigurationException {
String htmlPath = wordPath + File.separator + "html"
+ File.separator;
String htmlName = wordName + ".html";
final String imagePath = htmlPath + "image" + File.separator;
// 判断html文件是否存在,每次重新生成
File htmlFile = new File(htmlPath + htmlName);
// if (htmlFile.exists()) {
// return htmlFile.getAbsolutePath();
// }
// 原word文档
final String file = wordPath + File.separator + wordName + suffix;
InputStream input = new FileInputStream(new File(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) {
File imgPath = new File(imagePath);
if (!imgPath.exists()) {// 图片目录不存在则创建
imgPath.mkdirs();
}
File file = new File(imagePath + suggestedName);
try {
OutputStream os = new FileOutputStream(file);
os.write(content);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 图片在html文件上的路径 相对路径
return "image/" + suggestedName;
}
});
// 解析word文档
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
// 生成html文件上级文件夹
File folder = new File(htmlPath);
if (!folder.exists()) {
folder.mkdirs();
}
// 生成html文件地址
OutputStream outStream = new FileOutputStream(htmlFile);
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer serializer = factory.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
outStream.close();
return htmlFile.getAbsolutePath();
}
/**
* 2007版本word转换成html 2017-2-27
*
* @param wordPath
* word文件路径
* @param wordName
* word文件名称无后缀
* @param suffix
* word文件后缀
* @return
* @throws IOException
*/
public static String Word2007ToHtml(String wordPath, String wordName, String suffix)
throws IOException {
String htmlPath = wordPath + File.separator + "html"
+ File.separator;
String htmlName = wordName + ".html";
String imagePath = htmlPath + "image" + File.separator;
// 判断html文件是否存在
File htmlFile = new File(htmlPath + htmlName);
// if (htmlFile.exists()) {
// return htmlFile.getAbsolutePath();
// }
// word文件
File wordFile = new File(wordPath + File.separator + wordName + suffix);
// 1) 加载word文档生成 XWPFDocument对象
InputStream in = new FileInputStream(wordFile);
XWPFDocument document = new XWPFDocument(in);
// 2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
File imgFolder = new File(imagePath);
XHTMLOptions options = XHTMLOptions.create();
options.setExtractor(new FileImageExtractor(imgFolder));
// html中图片的路径 相对路径
options.URIResolver(new BasicURIResolver("image"));
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
// 3) 将 XWPFDocument转换成XHTML
// 生成html文件上级文件夹
File folder = new File(htmlPath);
if (!folder.exists()) {
folder.mkdirs();
}
OutputStream out = new FileOutputStream(htmlFile);
XHTMLConverter.getInstance().convert(document, out, options);
return htmlFile.getAbsolutePath();
}
public static void main(String[] args) throws Exception,
TransformerException, ParserConfigurationException {
System.out.println(Word2003ToHtml("C:/", "1", ".doc"));
// System.out.println(Word2007ToHtml("C:/", "docx", ".docx"));
}
}
需要的jar包
找相关jar包找了好久,测试了03和07版本的word文档,插入了简单图片,均可转换为HTML文件,复杂word文档未做测试。
参考来源 http://www.jb51.net/article/109307.htm
文档转成html在线预览,java poi Word文档转为HTML文件 实现在线预览功能相关推荐
- Word处理控件Aspose.Words功能演示:使用 Java 为 Word 文档添加水印
为防止非法使用.定义所有权.显示文档状态等多种目的,在 Word 文档中添加水印.在本文中,您将学习如何使用 Java 为 Word 文档添加水印.文章将分别演示如何将文字和图片水印插入到Word文档 ...
- vue如何在线预览及下载word文档
最近遇到个需求,说要在线预览及下载word文档,尝试了很多方法以后,发现有一个方法是比较好用的,故来分享一波,希望大家也可以在评论中告知更多实现的方法~ https://view.officeapps ...
- word文档打印预览有阴影_在打印预览中编辑Word文档
word文档打印预览有阴影 While viewing a Word document in the Print Preview window, you might notice a typo, or ...
- 使用java将word文档docx,doc(包含图形,文本框)完美转换成所有格式图片(pdf,png,gif,jpeg等等)
使用java将word文档docx,doc(包含图形,文本框,图片等)完美转换成所有格式图片(pdf,png,gif,jpeg等等)下文中附带代码,效果图等 思路 使用到的包 实现代码 效果图: 思路 ...
- Java 将word文档转成html内容,输出到富文本
使用Java 将word文档转成html内容,输出到富文本 上传word文档 解析到富文本 将word解析成html工具类 import com.common.utils.DateUtil; impo ...
- java将WORD文档转换成pdf文件
总结对jacob和Itext学习总结.本文试验的是将WORD转换成PDF文件. 实现思路 一.先将WORD文档转换成HMTL文件格式(参阅我的前一文<JAVA操作WORD文档). 二.用流读取H ...
- Java操作word文档将docx转换为pdf格式
Java操作word文档将docx转换为pdf格式 一.整体说明 在上传 Office 课件时,格式有:doc,docx,xls,xlsx,ppt,pptx,程序需要将其 转换成 pdf 格式, 才能 ...
- python分解word文档为多个_用python批量处理word文档
应我家领导要求,开发一个word文档批处理脚本,涉及word文档.excel表格.文件存取.排序与索引.简单GUI等内容,前期针对各分项功能实现写了几篇小文章,现在将总体思路记录一下,作为这个系列的完 ...
- Java将Word文档转换为PDF的完美工具
引用至:https://mp.weixin.qq.com/s/JIgo3f98HufGJx23mgtvag Java 将 Word 文档转换为 PDF 的完美工具 在日常工作中,PDF格式良好的视觉阅 ...
最新文章
- 一文看懂芯片测试产业
- 编程没基础学python多长时间-今天就来告诉你,没有编程基础的人适不适合学python...
- Mysql之group by 和order by 一起用时的排序问题(亲测)
- java 企业 网站源码 模版 屏幕自适应 有前后台 springmvc SSM 生成静态化
- spring http缓存_HTTP缓存与Spring示例
- joo工作流_不要错过使用jOOλ或jOOQ编写Java 8 SQL单行代码的机会
- 在内存中创建临时表和表变量
- MAC OS上将项目提交到github
- Entity Framework 实体框架的形成之旅--利用Unity对象依赖注入优化实体框架(2)
- java标识符_Java标识符的简单介绍
- Opencv之读取yuv420P
- tiny4412 串口驱动分析五 --- LDD3上TTY驱动程序源码
- 【Django 2021年最新版教程21】数据库查询 model 多条数据 queryset转dict字典 返回渲染到前端
- NOIP2016DAY1题解
- 仅15%的L2智能驾驶搭载DMS,「安全」背后的市场爆发在即
- Ant下载及配置安装
- http //t cn/xx 等链接的生成与还原
- ARM-LINUX-GCC交叉编译工具链必知必会
- 126企业邮箱smtp服务器设置,网易企业邮箱新增超级邮件列表等功能
- C语言网络编程:send函数详解
热门文章
- Chrome 100发布:启用全新图标,修复28个安全漏洞
- 时隔六年,FreeDOS终于更新,是否还能与Windows一战?
- 为什么学计算机的学生应该向开源项目做贡献?
- 程序员拒带电脑回家被开除获赔 19.4 万;库克称,很多功能来自中国消费者反馈;谷歌开源1.6万亿参数语言模型 | 极客头条...
- 阿里推出“阿里云网盘”App;Linux 发布 29 周年​| 极客头条
- 华为首秀 AI 全栈软件平台!
- 限时福利:入群锁定大会直播+PPT,听百位 AI 技术大咖、20 大热门主题分享!...
- 程序员需要了解的硬核知识之操作系统和应用
- 这款耳机性价比值得你看一下
- 程序员,活得是本事:30 岁后的 20 条人生建议