poi读取doc文档

导包:https://blog.csdn.net/u012488504/article/details/52996611

读取表格及字段及段落数:https://blog.csdn.net/qq578473688/article/details/72083684

java写入word进行换行:https://blog.csdn.net/ai_0922/article/details/82774359

java向word插入图片:

首先要明确一些概念:查询apache官网我们可以知道

HSSF/XSSF 是用于操作excel的

HSLF/XSLF是用于操作powerPoint的

HWPF/XWPF适用于操作word的 首先明确一下HWPF和XWPF

HWPF is the name of our port of the Microsoft Word 97(-2007) file format to pure Java. It also provides limited read only support for the older Word 6 and Word 95 file formats.

HWPF是用纯JAVA操作word97到07版本,同时可以查看word6和word95文件的格式。

The partner to HWPF for the new Word 2007 .docx format is XWPF. Whilst HWPF and XWPF provide similar features, there is not a common interface across the two of them at this time.

HWPF用于新的2007版本 以.docx格式的文件操作,HWPF和XWPF提供类似的特性,但是他们没有通用的接口

这里我们采用2007版及以下的使用HWPF

简单的文本提取,可以使用org.apache.poi.hwpf.extractor.WordExtractor,他可以接收inputsteam或者HWPFDocument的流。The getText()方法可以获得所有的段落,或者可以使用getParagraphText()依次从每个段落获取文本。另一个选项是getTextFromPieces(),它非常快,但是倾向于从页面返回非文本的内容,YMMV。

要获得特殊的文本,首先创建一个org.apache.poi.hwpf.HWPFDocument.使用getRange()获取范围,然后从中获取段落。然后可以获得文本和其他属性。

页眉页尾我们这里不做讨论

更改文本

可以通过Range对象的insertBefore()和 insertAfter() 方法修改值(对段落和字段也是同理)。同事也可以删除字段,大部分情况下,都能运行,并且很感激收到这个bug用于我们进行补丁!

HWPF不支持进行图片的操作,仅支持进行文本的操作,也就是说doc结尾的文件,我们都没办法利用poi进行图片的读写,而docx结尾的可以可以通过docx的文本进行修改。

docx文本修改参照文章:

www.cnblogs.com/duanrantao/p/8682897.html

因为处理的word文件为doc结尾的文件,所以,这里我们采用freemaker技术先生成模板代码,再添加内容形式:

可以参看 https://blog.csdn.net/afdasfggasdf/article/details/85275423

参考:

https://blog.csdn.net/qq_20039385/article/details/50893698

https://blog.csdn.net/whzhaochao/article/details/50440474

猜你喜欢

转载自blog.csdn.net/afdasfggasdf/article/details/85262021