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. css3 - 层次选择器

    div div { background: orange; } body>div { background: green; } .active+div { background: lime; } ...

  2. C# Select SelectMany 区别

    string[] text = { "Today is 2018-06-06", "weather is sunny", "I am happy&qu ...

  3. 图像处理算法2——Otsu最佳阈值分割法http://blog.csdn.net/xiaqunfeng123/article/details/17121195

    http://blog.csdn.net/xiaqunfeng123/article/details/17121195Otsu法是1979年由日本大津提出的.该方法在类间方差最大的情况下是最佳的,即统 ...

  4. 使用".."指定git提交范围与"..."指定git提交范围的区别

    http://blog.csdn.net/hansel/article/details/8952967 使用".."(两个点)和"..."(三个点)都可以指定一 ...

  5. HttpUtility.UrlEncode,Request.RawUrl,HttpUtility.UrlDecode,HttpUtility.UrlPathEncode,Uri.EscapeDataString

    碰到同样问题, 记录一下. 引自:https://www.cnblogs.com/ken-admin/p/5826480.html HttpUtility.UrlDecode(url),从Encode ...

  6. CSS入门学习

    一.What? CSS的全称是CascadingStyle Sheet,汉语意思是"级联样式表".通常又称为"风格样式表(StyleSheet)".它是用来进行 ...

  7. 关于TCP通信程序中数据的传递格式

    前言 在之前的回射程序中,实现了字符串的传递与回射.幸运的是,字符串的传递不用担心不同计算机类型的大小端匹配问题,然而,如果传递二进制数据,这就是一个要好好考虑的问题.在客户端和服务器使用不同的字节序 ...

  8. ASP.NET车辆管理系统100%源代码

    系统开发环境为VS2010.採用ASP.NET框架.数据库採用SQL Server.系统採用Ajax,具有:GPS导航(实时监控报警).申请审核.流程查看及短信息发送等功能.这个系统界面和功能是我认为 ...

  9. Colly provides a clean interface to write any kind of crawler/scraper/spider

    Scraping Framework for Golang http://go-colly.org/ https://github.com/gocolly/colly package main imp ...

  10. Android Studio 模拟器无法打开 emulator: ERROR: x86 emulation currently requires hardware

    首先要打开SDK的下载位置,找到以下陌路: android-sdk\extras\intel\Hardware_Accelerated_Execution_Manager\IntelHaxm.exe ...