10 import org.apache.poi.hwpf.HWPFDocument;
11 import org.apache.poi.hwpf.model.PicturesTable;
12 import org.apache.poi.hwpf.usermodel.CharacterRun;
13 import org.apache.poi.hwpf.usermodel.Picture;
14 import org.apache.poi.hwpf.usermodel.Range;
16 /**
17 * Provides access to the pictures both by offset, iteration over the
18 * un-claimed, and peeking forward
19 */
20 public class PicturesSource {//这个类是poi官网找的
21 private PicturesTable picturesTable;
22 private Set
output = new HashSet();
23 private Map lookup;
24 private List nonU1based;
25 private List all;
26 private int pn = 0;
28 public PicturesSource(HWPFDocument doc) {
29 picturesTable = doc.getPicturesTable();
30 all = picturesTable.getAllPictures();
32 // Build the Offset-Picture lookup map
33 lookup = new HashMap();
34 for (Picture p : all) {
35 lookup.put(p.getStartOffset(), p);
36 }
38 // Work out which Pictures aren't referenced by
39 // a \u0001 in the main text
40 // These are \u0008 escher floating ones, ones
41 // found outside the normal text, and who
42 // knows what else...
43 nonU1based = new ArrayList();
44 nonU1based.addAll(all);
45 Range r = doc.getRange();
46 for (int i = 0; i < r.numCharacterRuns(); i++) {
47 CharacterRun cr = r.getCharacterRun(i);
48 if (picturesTable.hasPicture(cr)) {
49 Picture p = getFor(cr);
50 int at = nonU1based.indexOf(p);
51 nonU1based.set(at, null);
52 }
53 }
54 }
56 private boolean hasPicture(CharacterRun cr) {
57 return picturesTable.hasPicture(cr);
58 }
60 private void recordOutput(Picture picture) {
61 output.add(picture);
62 }
64 private boolean hasOutput(Picture picture) {
65 return output.contains(picture);
66 }
68 private int pictureNumber(Picture picture) {
69 return all.indexOf(picture) + 1;
70 }
72 public Picture getFor(CharacterRun cr) {
73 return lookup.get(cr.getPicOffset());
74 }
76 /**
77 * Return the next unclaimed one, used towards the end
78 */
79 private Picture nextUnclaimed() {
80 Picture p = null;
81 while (pn < nonU1based.size()) {
82 p = nonU1based.get(pn);
83 pn++;
84 if (p != null)
85 return p;
86 }
87 return null;
88 }
2、处理图片和段落文字
1 package com.poi.test;
3 import java.io.ByteArrayOutputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
7 import org.apache.poi.hwpf.HWPFDocument;
8 import org.apache.poi.hwpf.model.PicturesTable;
9 import org.apache.poi.hwpf.usermodel.CharacterRun;
10 import org.apache.poi.hwpf.usermodel.Paragraph;
11 import org.apache.poi.hwpf.usermodel.Picture;
12 import org.apache.poi.hwpf.usermodel.Range;
14 public class PoiForWord {
15 /**
16 * 使用HWPFDocument解析word文档
17 * wps按doc处理即可
18 */
19 public void parseDocByHWPFDocument(){
20 try(FileInputStream is = new FileInputStream(new File("c:\\a.wps"));HWPFDocument document = new HWPFDocument(is);){
21 ByteArrayOutputStream baos = new ByteArrayOutputStream();//字节流,用来存储图片
22 PicturesSource pictures = new PicturesSource(document);
23 PicturesTable pictureTable = document.getPicturesTable();
25 Range r = document.getRange();//区间
26 for(int i=0;i<r.numParagraphs();i++){
27 Paragraph p = r.getParagraph(i);//段落
28 int fontSize = p.getCharacterRun(0).getFontSize();//字号,字号和是否加粗可用来当做标题或者某一关键标识的判断
boolean isBold = p.getCharacterRun(0).isBold();//是否加粗
29 String paragraphText = p.text();//段落文本
31 //以下代码解析图片,这样获取的图片是在文档流中的,是和文本按顺序解析的,可以很好的解决图片定位问题
32 for(int j=0;j<p.numCharacterRuns();j++){
33 CharacterRun cr = p.getCharacterRun(j);//字符
34 if(pictureTable.hasPicture(cr)){
35 Picture picture = pictures.getFor(cr);
36 //如果是在页面显示图片,可转换为base64编码的图片
37 picture.writeImageContent(baos);//将图片写入字节流
38 // String base64Image = "<img src='data:image/png;base64,"+new BASE64Encoder().encode(baos.toByteArray())+"'/>";
39 }
40 }
41 }
42 }catch(Exception e){
43 e.printStackTrace();
44 }
45 }
3、处理表格
5 @Test
6 public void parseDocTableByHWPFDocument(){
7 try(FileInputStream is = new FileInputStream(new File("d:\\b.doc"));HWPFDocument document = new HWPFDocument(is);){
8 Range r = document.getRange();//区间
9 for(int i=0;i<r.numParagraphs();i++){
10 Paragraph p = r.getParagraph(i);//段落
11 String text = p.text();
13 if(text.indexOf("序号")!=-1){//解析表格需要从表格第一个单元格获取表格,另一种表格的方式是直接获取所有表格,但是无法判断表格在文档中的位置
14 Table table = r.getTable(p);
16 int numRows = table.numRows();//获取行数
18 for(int j=0;j<numRows;j++){
19 TableRow row = table.getRow(j);
20 int numCells = row.numCells();//当前行列数
21 for(int k=0;k<numCells;k++){
22 TableCell cell = row.getCell(k);
23 System.out.print(cell.text()+" @ ");
24 }
25 System.out.println();
26 }
27 }
28 }
29 }catch(Exception e){
30 e.printStackTrace();
31 }
32 }
字符"?"可通过字符串替换或截取来解决
另一种解析的方式,只支持解析文本内容,且无法获取字号和加粗等字体格式
1 WordExtractor extor = new WordExtractor(is);
2 String[] paragraphText = extor.getParagraphText();
public JsonResult readWordTemporaryExit(String filePath) throws Exception {
JsonResult jsonResult = new JsonResult();
PageData pd = new PageData();
try {
FileInputStream
1、H
WPF
Document
通过读入一个已存在
DOC
文件,加载一个Word
文档
【似乎没有发现直接创建一个
文档
的方法】
FileInputStreamin=newFileInputStream("C:\\blank.
doc
");
H
WPF
Document
doc
=newH
WPF
Document
(in);
2、Range、Paragraph、Section是操作Wo
在使用POI写word
doc
文件的时候我们必须要先有一个
doc
文件才行,因为我们在写
doc
文件的时候是通过H
WPF
Document
来写的,而H
WPF
Document
是要依附于一个
doc
文件的。所以通常的做法是我们先在硬盘上准备好一个内容空白的
doc
文...
读word
doc
文件2中方式
1.1 通过WordExtractor读文件(在WordExtractor内部进行信息
读取
时还是通过H
WPF
Document
来获取的。)
1.2 通过H
WPF
Document
读文件
Apache poi的h
wpf
模块是专门用来对word
doc
文件进行读写操作的。在h
wpf
里面我们使用H
WPF
Document
来表示一个word
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/82774...
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
二,准备好w...
求教:怎么使用poi h
wpf
接口操作
doc
文档
,重点:需要操作
doc
的Node
比如说
doc
x中可以用Node Node = XWPatagraph.getCTP().getDomNode();然后我们就可以用这个node做好多xml节点操作了。我的问题:现在需要兼容
doc
格式
文档
,用的H
WPF
Document
这一套接口,请问有没有配套这个接口的能操作
doc
节点node的api方案,求告知,万分感谢啊。
我的需求:在
doc
文档
中的指定内容处加书签。
import org.apache.poi.h
wpf
.H
WPF
Document
;
import org.apache.poi.h
wpf
.usermodel.CharacterRun;
import org.apache.poi.h
wpf
.usermodel.Paragraph;
import org.apache.poi.h
wpf
.usermodel.Range;
import org.apach
前言:如果你可爱的项目经理要求安卓端的你来操作word实现各种功能,不要犹豫,直接动之以情晓之以理,因为这本来就是java的poi,安卓虽然源自java,但对于java的很多东西是不支持的,已有的各种jar包也不方便更改,各种报错会搞的你脑阔疼。所以编辑word
文档
这种事让后台来做要比安卓来做简单的多,但如果实在避免不了,接着,给你代码。
说明:本篇不支持word2007版,只支持2003版,也...
import org.apache.poi.POIXML
Document
;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.h
wpf
.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache....