apache poi操作(适用于word 2007)

适用于word 2007 poi 版本 3.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

/**
* 适用于word 2007 poi 版本 3.7
*/
public class WordPoiUtil {

/**
* 根据指定的参数值、模板,生成 word 文档
*
* @param param
* 需要替换的变量
* @param template
* 模板
*/
public static XWPFDocument generateWord(Map<String, Object> param,
String template) {
XWPFDocument doc = null;
try {
OPCPackage pack = POIXMLDocument.openPackage(template);
doc = new XWPFDocument(pack);
if (param != null && param.size() > 0) {

// 处理段落
List<XWPFParagraph> paragraphList = doc.getParagraphs();
processParagraphs(paragraphList, param, doc);

// 处理表格
Iterator<XWPFTable> it = doc.getTablesIterator();
while (it.hasNext()) {
XWPFTable table = it.next();
List<XWPFTableRow> rows = table.getRows();
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
List<XWPFParagraph> paragraphListTable = cell
.getParagraphs();
processParagraphs(paragraphListTable, param, doc);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}

/**
* 处理段落
*
* @param paragraphList
*/
public static void processParagraphs(List<XWPFParagraph> paragraphList,
Map<String, Object> param, XWPFDocument doc) {
if (paragraphList != null && paragraphList.size() > 0) {
for (XWPFParagraph paragraph : paragraphList) {
boolean addReplace = false;
List<XWPFRun> runs = paragraph.getRuns();
//每个需要替换的key的run的位置的集合
List<Integer> replaceRuns = new ArrayList<Integer>();
//每个段落的所有的key run的集合
List<List<Integer>> perReplaceRunList = new ArrayList<List<Integer>>();
for (int i = 0; i< runs.size();i++){
String text = runs.get(i).getText(0);
if(addReplace){
replaceRuns.add(i);
}
if(text != null && text.contains("#")){
addReplace = true;
replaceRuns.add(i);
}
if(text != null && text.contains("}")){
addReplace = false;
perReplaceRunList.add(replaceRuns);
replaceRuns = new ArrayList<Integer>();
}
}

for(int i=0;i<perReplaceRunList.size();i++){
List<Integer> runsList = perReplaceRunList.get(i);
System.out.println("==========================");
StringBuffer textSb = new StringBuffer();
for(int j = 0;j<runsList.size();j++){
System.out.println("============replace_runs"+runs.get(runsList.get(j)).getText(0));
textSb.append(runs.get(runsList.get(j)).getText(0));
}
String replaceStr = textSb.toString();
for(int j = 0; j<runsList.size();j++){
for (Entry<String, Object> entry : param.entrySet()) {
String key = entry.getKey();
if (replaceStr.indexOf(key) != -1) {
Object value = entry.getValue();
if (value instanceof String) {// 文本替换
replaceStr = replaceStr.replace(key, value.toString());
}
}
}
}
System.out.println("==========="+replaceStr);
for(int j = 0;j<runsList.size();j++){
if(j == 0){
runs.get(runsList.get(j)).setText(replaceStr, 0);
}else{
runs.get(runsList.get(j)).setText("", 0);
}
}
for(int j = 0;j<runsList.size();j++){
System.out.println("============转换后"+runs.get(runsList.get(j)).getText(0));
}

}

}
}
}

public static List<String> getReplaceFields(String template){
List<String> replaceFields = new ArrayList<String>();
XWPFDocument doc = null;
try {
OPCPackage pack = POIXMLDocument.openPackage(template);
doc = new XWPFDocument(pack);
// 处理段落
List<XWPFParagraph> paragraphList = doc.getParagraphs();
replaceFields.addAll(getFields(paragraphList));

// 处理表格
Iterator<XWPFTable> it = doc.getTablesIterator();
while (it.hasNext()) {
XWPFTable table = it.next();
List<XWPFTableRow> rows = table.getRows();
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
List<XWPFParagraph> paragraphListTable = cell
.getParagraphs();
replaceFields.addAll(getFields(paragraphListTable));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return replaceFields;
}

/**
* 获取段落的需要替换的字段
* @param paragraphList
* @return
*/
public static List<String> getFields(List<XWPFParagraph> paragraphList) {
List<String> fieldList = new ArrayList<String>();
if (paragraphList != null && paragraphList.size() > 0) {
for (XWPFParagraph paragraph : paragraphList) {
boolean addReplace = false;
List<XWPFRun> runs = paragraph.getRuns();
//每个需要替换的key的run的位置的集合
List<Integer> replaceRuns = new ArrayList<Integer>();
//每个段落的所有的key run的集合
List<List<Integer>> perReplaceRunList = new ArrayList<List<Integer>>();
for (int i = 0; i< runs.size();i++){
String text = runs.get(i).getText(0);
if(addReplace){
replaceRuns.add(i);
}
if(text != null && text.contains("#")){
addReplace = true;
replaceRuns.add(i);
}
if(text != null && text.contains("}")){
addReplace = false;
perReplaceRunList.add(replaceRuns);
replaceRuns = new ArrayList<Integer>();
}
}

for(int i=0;i<perReplaceRunList.size();i++){
List<Integer> runsList = perReplaceRunList.get(i);
System.out.println("==========================");
StringBuffer textSb = new StringBuffer();
for(int j = 0;j<runsList.size();j++){
System.out.println("============replace_runs"+runs.get(runsList.get(j)).getText(0));
textSb.append(runs.get(runsList.get(j)).getText(0));
}
String replaceStr = textSb.toString().trim();
System.out.println("====replaceStr=" + replaceStr.substring(replaceStr.indexOf("#")+2,replaceStr.length()-1));
// System.out.println(replaceStr.substring(2,replaceStr.length()-1));
fieldList.add(replaceStr.substring(replaceStr.indexOf("#")+2,replaceStr.length()-1));

}

}
}
return fieldList;
}
}

坚持原创技术分享,您的支持将鼓励我继续创作!