定义一个工具类wordUtil
package com.keji.service.utils;
import org.apache.poi.xwpf.usermodel.*;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//from fhadmin.cn
public class docUtil {
public static XWPFDocument replaceDoc(String srcPath, Map<String, Object> param) {
try {
// 读取word模板
InputStream fis = new FileInputStream(srcPath);
XWPFDocument doc = new XWPFDocument(fis);
//处理段落
List<XWPFParagraph> paragraphList = doc.getParagraphs();
processParagraph(paragraphList,doc,param);
//处理表格
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();
processParagraph(paragraphListTable, doc, param);
}
}
}
return doc;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void processParagraph(List<XWPFParagraph> paragraphList, XWPFDocument doc, Map<String, Object> param) {
if(paragraphList != null && paragraphList.size() > 0) {
for (XWPFParagraph paragraph : paragraphList) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
String runString = run.toString();
Matcher m = Pattern.compile("\\$\\{(.*?)}").matcher(runString);
if (m.find()) {
boolean isSetText = false;
for (Map.Entry<String, Object> entry : param.entrySet()) {
String key = entry.getKey();
if (runString.indexOf(key) != -1) {
isSetText = true;
Object value = entry.getValue();
if (entry.getValue() == null || entry.getValue().equals("")){
value = "未填写";
}
runString = runString.replace(key, value.toString());
}
}
if (isSetText) {
run.setText(runString, 0);
}
}
}
}
}
}
}
Map<String, Object> map=new HashMap<String, Object>();
// 文档里面的列名为 ${name} 和 ${sex}
map.put (“${name}”, “姓名”);
map.put (“${sex}”, “性别”);
// 文件位置和 map
XWPFDocument doc = docUtil.replaceDoc(“D:\word\month.docx”, map);
// 文件保存位置
OutputStream os = new FileOutputStream(“D:\tttt.doc”);
doc.write(os);
os.close();
System.out.println (“输出成功!”);