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的更多相关文章

  1. java使用poi读取doc和docx文件(maven自动导入依赖包)

    java使用poi读取doc和docx文件(maven自动导入依赖包) 于是在网上搜寻了一阵之后才发现原来doc文档和excel一样不能用普通的io流的方法来读取,而是也需要用poi,于是进行了一番尝 ...

  2. POI读写Word docx文件

    使用POI读写word docx文件 目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx ...

  3. 使用POI读写word docx文件

    目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx文件 2.1     直接通过XWPF ...

  4. POI读word docx 07 文件的两种方法

    POI在读写word docx文件时是通过xwpf模块来进行的,其核心是XWPFDocument.一个XWPFDocument代表一个docx文档,其可以用来读docx文档,也可以用来写docx文档. ...

  5. IOS 使用webview 显示 doc/docx/xls/pdf等

    在一款项目里添加阅读各种文档功能 那么对在线的文档或者是下载后的文档 进行阅读,比如 doc/docx/xls/pdf等文件 有两种方法总结如下: 1. - (void)viewDidLoad { [ ...

  6. 使用poi读取word2007(.docx)中的复杂表格

    使用poi读取word2007(.docx)中的复杂表格 最近工作需要做一个读取word(.docx)中的表格,并以html形式输出.经过上网查询,使用了poi. 对于2007及之后的word文档,需 ...

  7. java使用poi读取doc和docx文件

    这几天在学习java io流的东西,有一个网友看到博客后问了一个问题,就是说他的doc文档为什么用我所说的方法死活就是乱码. 我一开始以为是他方法问题,结果自己试了之后发现和他的结果一样也是乱码. 于 ...

  8. poi读写doc和docx

    https://www.cnblogs.com/always-online/p/4800131.html POI是 Apache 旗下一款读写计算机中的 word 以及 excel 文件的工具. po ...

  9. poi word 转html (.DOC .DOCX )

    注:不支持图片,支持表格 package com.bjhy.platform.report.commons; import java.io.BufferedWriter; import java.io ...

随机推荐

  1. cache数据库之表的存储结构

    1.我们已经建了一个person类,接下来就是表的存储结构 2.打开Inspector,先输入rowid名字为p_RowID,选class->Storage 3.新建一个Storage,选择Ca ...

  2. 关于C语言中二维数组传參————————【Badboy】

    直接上代码: #include void Fun(int *a[],int m,int n)// { printf("%d\t",*a);//[0][0] /* int e[2][ ...

  3. Python 模块之 ConfigParser: 用 Python 解析配置文件

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做 ...

  4. Cocos2d-X中提高性能的方法

     1)内存使用效率: 使用大纹理 场景切换时,要尽量使用replaceScene 2)用好缓存: CCTextureCache(纹理缓存) CCSpriteFrameCache(精灵帧缓存) CC ...

  5. Java爬虫快速开发工具uncs的部署攻略

    写在前面 uncs是java快速开发爬虫的工具,简单便捷,经过大量版本迭代和生产验证,可以适用大多数网站,推荐使用. 一.基本用法 1.1 开发包获取 目前只能在公司内网maven服务器获取到 < ...

  6. openwrt spi flash 分区适配过程

    openwrt spi flash 分区适配过程 这里基于 openwrt mt7620a 平台来跟踪,主要是想理清 dts 里的分区描述是如何一步步转化成内核分区行为. 先来看看 dts 中关于分区 ...

  7. Html调用 QQ接口

    <A href="tencent://message/?uin=1805843351&Site=有事Q我&Menu=yes"> <img styl ...

  8. IOS版App的控件元素定位

    前言 Android版App的控件元素可以通过Android studio自带的工具uiautomatorviewer来协助定位! IOS版App的控件元素可以通过Appium来实现(未实现),或ap ...

  9. python中if __name__ == '__main__': 的解析(转载)

    当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__.一个 ...

  10. C# GetHashCode 的实现方式

    在项目中,在使用哈希表时.有时会须要Override GetHashCode. 这里给出一种普遍的做法: 版本号1:实现一个helper.传递类型T.返回这个类型的hashcode.函数逻辑非常直接, ...