在POI中还存在有针对于word doc文件进行格式转换的功能。我们可以将word的内容转换为对应的Html文件,也可以把它转换为底层用来描述doc文档的xml文件,还可以把它转换为底层用来描述doc文档的xml格式的text文件。这些格式转换都是通过AbstractWordConverter特定的子类来完成的。

1 转换为Html文件

将doc文档转换为对应的Html文档是通过WordToHtmlConverter类进行的。它会尽量的利用Html的方式来呈现原文档的样式。示例代码:

   /**
* Word转换为Html
* @throws Exception
*/
@Test
public void testWordToHtml() throws Exception {
InputStream is = new FileInputStream("D:\\test.doc");
HWPFDocument wordDocument = new HWPFDocument(is);
WordToHtmlConverter converter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//对HWPFDocument进行转换
converter.processDocument(wordDocument);
Writer writer = new FileWriter(new File("D:\\converter.html"));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
//是否添加空格
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
transformer.setOutputProperty( OutputKeys.METHOD, "html" );
transformer.transform(
new DOMSource(converter.getDocument() ),
new StreamResult( writer ) );
}

2 转换为Xml文件

将doc文档转换为对应的Xml文件是通过WordToFoConverter类进行的。它可以把doc文档转换为底层用来描述doc文档的Xml文档。示例代码:

   /**
* Word转Fo
* @throws Exception
*/
@Test
public void testWordToFo() throws Exception {
InputStream is = new FileInputStream("D:\\test.doc");
HWPFDocument wordDocument = new HWPFDocument(is);
WordToFoConverter converter = new WordToFoConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//对HWPFDocument进行转换
converter.processDocument(wordDocument);
Writer writer = new FileWriter(new File("D:\\converter.xml"));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
//是否添加空格
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
// transformer.setOutputProperty( OutputKeys.METHOD, "html" );
transformer.transform(
new DOMSource(converter.getDocument() ),
new StreamResult( writer ) );
}

3  转换为Text文件

将doc文档转换为text文档是通过WordToTextConverter来进行的。它可以把doc文档转换为底层用于描述doc文档的Xml格式的text文档。示例代码:

   /**
* Word转换为Text
* @throws Exception
*/
@Test
public void testWordToText() throws Exception {
InputStream is = new FileInputStream("D:\\test.doc");
HWPFDocument wordDocument = new HWPFDocument(is);
WordToTextConverter converter = new WordToTextConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//对HWPFDocument进行转换
converter.processDocument(wordDocument);
Writer writer = new FileWriter(new File("D:\\converter.txt"));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
//是否添加空格
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
transformer.setOutputProperty( OutputKeys.METHOD, "text" );
transformer.transform(
new DOMSource(converter.getDocument() ),
new StreamResult( writer ) );
}

POI转换word doc文件为(html,xml,txt)的更多相关文章

  1. 使用POI转换word doc文件

    目录 1       转换为Html文件 2       转换为Xml文件 3       转换为Text文件 在POI中还存在有针对于word doc文件进行格式转换的功能.我们可以将word的内容 ...

  2. 使用POI读写Word doc文件

    使用POI读写word doc文件 目录 1     读word doc文件 1.1     通过WordExtractor读文件 1.2     通过HWPFDocument读文件 2     写w ...

  3. android使用POI读写word doc文件

    目录 1     读word doc文件 1.1     通过WordExtractor读文件 1.2     通过HWPFDocument读文件 2     写word doc文件 Apache p ...

  4. 解决 apache poi 转换 word(docx) 文件到 html 文件表格没边框的问题

    一.起因 这几天在做电子签章问题,要通过替换docx文件中的占位符生成包含业务数据的合同数据,再转换成html文件,转换成pdf文件.遇到的问题是:通过apache poi转换docx到html时,原 ...

  5. POI读word doc 03 文件的两种方法

    Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的.在hwpf里面我们使用HWPFDocument来表示一个word doc文档.在HWPFDocument里面有这么几个 ...

  6. POI写入word doc 03 模板的实例

    在使用POI写word doc文件的时候我们必须要先有一个doc文件才行,因为我们在写doc文件的时候是通过HWPFDocument来写的,而HWPFDocument是要依附于一个doc文件的.所以通 ...

  7. POI读写Word docx文件

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

  8. VBA/VBScript提取Word(*.doc)文件中包含的图片(照片)

    VBA/VBScript提取Word(*.doc)文件中包含的图片(照片)   要处理的人事简历表是典型的Word文档,其中一人一份doc,里面包含有个人的照片,如果要把里面的照片复制出来就比较麻烦了 ...

  9. 15个最好的PDF转word的在线转换器,将PDF文件转换成doc文件

    PDF是一种文件格式,包含文本,图像,数据等,这是独立于操作系统的文件类型.它是一个开放的标准,压缩,另一方面DOC文件和矢量图形是由微软文字处理文件.该文件格式将纯文本格式转换为格式化文档.它支持几 ...

随机推荐

  1. 如何将基于对话框的MFC工程改成基于BCG的

    1.stdafx.h 加入如下内容.BCGCBProInc.h间接导入了lib. 2.应用程序类的父类由CWinApp改成CBCGPWinApp.构造函数增加如下代码: 3.对话框的父类有CDialo ...

  2. opencv surf特征点匹配拼接源码

    http://blog.csdn.net/huixingshao/article/details/42672073 /** * @file SURF_Homography * @brief SURF ...

  3. [Xcode 实际操作]九、实用进阶-(25)使用Storyboard(故事版)的约束功能,使项目快速适配各种分辨率的设备

    目录:[Swift]Xcode实际操作 本文将演示使用故事版的约束功能,使项目快速适配各种分辨率的设备. 在项目导航区打开并编辑主故事版[Main.storyboard]. 在当前故事版中,已经存在一 ...

  4. 指向函数的指针和block

    原文网址: http://www.cnblogs.com/cxbblog/p/3841226.html 一:block基础知识 block基础知识 基本概念:block是用来保存一段代码的:^:是bl ...

  5. pycharm 中切换虚拟环境

    在pycharm上创建虚拟环境,网上的资料非常多. 如果pycharm上有多个项目,如何切换每个项目的虚拟环境? cmd 命令进入虚拟环境所在的文件夹(Pycharm在每创建一个新项目时就会创建一个虚 ...

  6. SQL COUNT DISTINCT 函数

    定义和用法 可以一同使用 DISTINCT 和 COUNT 关键词,来计算非重复结果的数目. 语法 SELECT COUNT(DISTINCT column(s)) FROM table 例子 注意: ...

  7. Jquery | 基础 | .hover()

    https://api.jquery.com/hover/#hover-handlerIn-handlerOut http://jquery.cuishifeng.cn/hover.html

  8. Tinghua Data Mining 4

    贝叶斯 决策树 知道三文鱼和金枪鱼颜色 让你去猜 B命中的概率不能直接减去四分之三 因为有可能同时命中 A B 命中不是互斥事件 即便体检报告是阳性,真正得癌症的概率也很小,只有0.21 绝大多数的阳 ...

  9. MySQL在远程访问时非常慢的解决skip-name-resolve

    服务器放在局域网内进行测试时,数据库的访问速度还是很快.但当服务器放到外网后,数据库的访问速度就变得非常慢. 后来在网上发现解决方法,my.cnf里面添加 [mysqld] skip-name-res ...

  10. winfrom项目的打印

    自己可以下一个PDF打印机(例如下载64位office虚拟打印文档) 首先要添加控件 1.添加打印的选项卡,并命名为打印 2.点击打印选项卡,右击鼠标,选择选择项 using System;using ...