POI实现DOC/DOCX转HTML
1.使用HWPF处理DOC
public class DocToHtml {
private static final String encoding = "UTF-8";
public static String convert2Html(String wordPath)
throws FileNotFoundException, TransformerException, IOException,
ParserConfigurationException {
if( wordPath == null || "".equals(wordPath) ) return "";
File file = new File(wordPath);
if( file.exists() && file.isFile() )
return convert2Html(new FileInputStream(file));
else
return "";
}
public static String convert2Html(String wordPath, String context)
throws FileNotFoundException, TransformerException, IOException,
ParserConfigurationException {
if( wordPath == null || "".equals(wordPath) ) return "";
File file = new File(wordPath);
if( file.exists() && file.isFile() )
return convert2Html(new FileInputStream(file), context);
else
return "";
}
public static String convert2Html(InputStream is)
throws TransformerException, IOException,
ParserConfigurationException {
return convert2Html(is, "");
}
public static String convert2Html(InputStream is, HttpServletRequest req) throws TransformerException, IOException, ParserConfigurationException {
return convert2Html(is, req.getContextPath());
}
public static String convert2Html(InputStream is, final String context) throws IOException, ParserConfigurationException, TransformerException {
HWPFDocument wordDocument = new HWPFDocument(is);
WordToHtmlConverter converter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
final String prefix = sdf.format(new Date());
final Map<Object, String> suffixMap = new HashMap<Object, String>();
converter.setPicturesManager(new PicturesManager() {
public String savePicture(byte[] content, PictureType pictureType,
String suggestedName, float widthInches, float heightInches) {
String prefixContext = context.replace("\\", "").replace("/", "");
prefixContext = StringUtils.isNotBlank(prefixContext) ? "/" + prefixContext + "/" : prefixContext;
suffixMap.put(new String(content).replace(" ", "").length(), suggestedName);
return prefixContext
+ UeConstants.VIEW_IMAGE_PATH + "/" + UeConstants.UEDITOR_PATH
+ "/" + UeConstants.UEDITOR_IMAGE_PATH + "/"
+ prefix + "_"
+ suggestedName;
}
});
converter.processDocument(wordDocument);
List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for(Picture pic : pics) {
try {
pic.writeImageContent(new FileOutputStream(
UeConstants.IMAGE_PATH
+ "/" + prefix + "_" + suffixMap.get(new String(pic.getContent()).replace(" ", "").length())));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
StringWriter writer = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, encoding);
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(
new DOMSource(converter.getDocument()),
new StreamResult(writer) );
writer.close();
return writer.toString();
}
}
2.使用XWPFDocument处理DOCX
public class XHTMLConverterTestCase
extends AbstractXWPFPOIConverterTest
{ protected void doGenerate( String fileInName )
throws IOException
{
doGenerateSysOut( fileInName );
doGenerateHTMLFile( fileInName );
} protected void doGenerateSysOut( String fileInName )
throws IOException
{ long startTime = System.currentTimeMillis(); XWPFDocument document = new XWPFDocument( AbstractXWPFPOIConverterTest.class.getResourceAsStream( fileInName ) ); XHTMLOptions options = XHTMLOptions.create().indent( 4 );
OutputStream out = System.out;
XHTMLConverter.getInstance().convert( document, out, options ); System.err.println( "Elapsed time=" + ( System.currentTimeMillis() - startTime ) + "(ms)" );
} protected void doGenerateHTMLFile( String fileInName )
throws IOException
{ String root = "target";
String fileOutName = root + "/" + fileInName + ".html"; long startTime = System.currentTimeMillis(); XWPFDocument document = new XWPFDocument( AbstractXWPFPOIConverterTest.class.getResourceAsStream( fileInName ) ); XHTMLOptions options = XHTMLOptions.create();// .indent( 4 );
// Extract image
File imageFolder = new File( root + "/images/" + fileInName );
options.setExtractor( new FileImageExtractor( imageFolder ) );
// URI resolver
options.URIResolver( new FileURIResolver( imageFolder ) ); OutputStream out = new FileOutputStream( new File( fileOutName ) );
XHTMLConverter.getInstance().convert( document, out, options ); System.out.println( "Generate " + fileOutName + " with " + ( System.currentTimeMillis() - startTime ) + " ms." );
}
}
项目下载地址:http://download.csdn.net/detail/luka2008/7902285
本文转自:http://blog.csdn.net/luka2008/article/details/21168287
POI实现DOC/DOCX转HTML的更多相关文章
- java使用poi读取doc和docx文件(maven自动导入依赖包)
java使用poi读取doc和docx文件(maven自动导入依赖包) 于是在网上搜寻了一阵之后才发现原来doc文档和excel一样不能用普通的io流的方法来读取,而是也需要用poi,于是进行了一番尝 ...
- POI读写Word docx文件
使用POI读写word docx文件 目录 1 读docx文件 1.1 通过XWPFWordExtractor读 1.2 通过XWPFDocument读 2 写docx ...
- 使用POI读写word docx文件
目录 1 读docx文件 1.1 通过XWPFWordExtractor读 1.2 通过XWPFDocument读 2 写docx文件 2.1 直接通过XWPF ...
- POI读word docx 07 文件的两种方法
POI在读写word docx文件时是通过xwpf模块来进行的,其核心是XWPFDocument.一个XWPFDocument代表一个docx文档,其可以用来读docx文档,也可以用来写docx文档. ...
- IOS 使用webview 显示 doc/docx/xls/pdf等
在一款项目里添加阅读各种文档功能 那么对在线的文档或者是下载后的文档 进行阅读,比如 doc/docx/xls/pdf等文件 有两种方法总结如下: 1. - (void)viewDidLoad { [ ...
- 使用poi读取word2007(.docx)中的复杂表格
使用poi读取word2007(.docx)中的复杂表格 最近工作需要做一个读取word(.docx)中的表格,并以html形式输出.经过上网查询,使用了poi. 对于2007及之后的word文档,需 ...
- java使用poi读取doc和docx文件
这几天在学习java io流的东西,有一个网友看到博客后问了一个问题,就是说他的doc文档为什么用我所说的方法死活就是乱码. 我一开始以为是他方法问题,结果自己试了之后发现和他的结果一样也是乱码. 于 ...
- poi读写doc和docx
https://www.cnblogs.com/always-online/p/4800131.html POI是 Apache 旗下一款读写计算机中的 word 以及 excel 文件的工具. po ...
- poi word 转html (.DOC .DOCX )
注:不支持图片,支持表格 package com.bjhy.platform.report.commons; import java.io.BufferedWriter; import java.io ...
随机推荐
- Linux 软件大全
应用 音频 Airtime - Airtime 是一款用于调度和远程站点管理的开放广播软件 Ardour - 在 Linux 上录音,编辑,和混音 Audacious - 开源音频播放器,按你想 ...
- NGUI版虚拟摇杆
以下是我用nui实现的一个虚拟摇杆. 1,示图 2.代码例如以下,都有比較具体的凝视.就不说明了. using UnityEngine; using System.Collections; using ...
- hdu1034 简单模拟
这里开一个二维数组.num[105][2]; 我也不知道N有多少,随便开的, 那么这里num[i][0] 表示当前 第 i 个人拥有的糖果数,num[i][1]表示他上面一个人分给他的糖果数.详 ...
- windows程序设计——飞机大战笔记(Access数据库的使用)
//////////////////2015/07/22/////////////////// /////////////////by xbw ///////////////////////// // ...
- windowsclient开发--使你的client执行时记住上次关闭的大小和位置
差点儿全部的windowsclient都能够调整大小,所以用户依据自己的喜好调整client的大小和位置. 可是当该client退出后,又一次执行client的时候.我们往往又要调整自己喜好的大小和位 ...
- [笔记] sed and awk
/ awk程序的典型示例是将数据转换成格式化的报表,当数据拥有某种结构时就能最好的体现awk的好处:可以使用awk脚本对数据的列重新排序,甚至可以将列变成行以及将行变成列:awk的功能将文本编辑的思想 ...
- 测试 MD
上面是一张图片 总店?
- Swift的两个小窍门
一:查看Swift版本号(How do I see which version of Swift I’m using in Xcode?) 终端下输入:xcrun swift -version(in ...
- dwr框架中DWRUtil的方法
dwr框架中DWRUtil的方法 2008-10-14 17:57:23| 分类: JAVA | 标签: |举报 |字号大中小 订阅 7. util.js 功能 util.js包含了一些工 ...
- 搭建基于Jenkins的CI服务器
安装Jenkins和创建任务这些操作网上一搜一大把,这里就没必要写了,直接就开始编译.单元测试,覆盖,git提交触发构建,构建失败发送给提交人邮件. 因为项目比较复杂,为了懒省事我直接在CI服务器上安 ...