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 ...
随机推荐
- tomcat7设置usernamepassword
因为tomcat是绿色版.今天想在网页上管理项目,却发现没实username和password.打开tomcat-users.xml文件全都是凝视.如图: 将例如以下代码拷贝到tomcat-users ...
- linux 设置ssh密钥登录
一.查看服务器上是否开启了sshd服务 要确认linux是不是安装了和开启了ssh服务可以输入 netstat -an|grep 22,查看22端口是否打开,如果找开,则证明SSH服务开启了. ps ...
- Item 51:写new和delete时请遵循惯例
Item 51: Adhere to convention when writing new and delete. Item 50介绍了怎样自己定义new和delete但没有解释你必须遵循的惯例. ...
- 微信小程序日期定位弹出框遮挡问题
只需要用bindtap绑定一个点击后的操作(隐藏键盘): wx.hideKeyboard()
- php如何读取ini文件
很多时候,我们使用配置文件来读取配置,那么php如何使用ini文件呢? 代码如下: 例如将:数据库信息存到ini文件中,进行读取. <?php header('content-type:text ...
- OS: 读者写者问题(写者优先+LINUX+多线程+互斥量+代码)(转)
一. 引子 最近想自己写个简单的 WEB SERVER ,为了先练练手,熟悉下在LINUX系统使用基本的进程.线程.互斥等,就拿以前学过的 OS 问题开开刀啦.记得当年学读者写者问题,尤其是写者优先的 ...
- GOOGLE VR SDK开发VR游戏,VR播放器之中的一个
近期一年来,VR虚拟现实和AR增强现实技术的宣传甚嚣尘上.事实上VR,AR技术非常早就有了,一直没有流行开来.不可否认价格是影响技术推广的最大壁垒. 谷歌对VR最大的贡献是提供了便宜的谷歌眼镜,依照G ...
- 【转载】ASP和ASP.NET根本区别
ASP.NET和ASP的最大区别在于编程思维的转换,而不仅仅在于功能的增强.ASP使用VBS/JS这样的脚本语言混合html来编程,而那些脚本语言属于弱类型.面向结构的编程语言,而非面向对象,这就明显 ...
- DateTime操作,时间范围,加减
DB里边存的是char类型数组的时间,例如20151111 12171220000,现在需要把这个时间加减5s,组成 一个时间范围 然后再写存储过程. 想到的办法就是把这个时间先转换成DateTime ...
- Class doesn't implement Cloneable之怪象
1. 报错: -20 ::): threadid=: thread exiting with uncaught exception (group=0x4001d400) -20 ::): FATAL ...