定义一个工具类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;
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 = https://www.cnblogs.com/h-w-b/p/entry.getValue();
if (entry.getValue() == null || entry.getValue().equals("")){
value = "https://www.cnblogs.com/h-w-b/p/未填写";
}
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("输出成功!");
最后会有一个奇怪的bug,word档案里的${name}如果是手打上去就可以用,如果是复制上去就会被识别为三个字符串,也就是不能替换,当然也有解决方法,可以把全文复制到win自带的写字板(不需要调任何格式),
然后把原文删了复制回来就行了,复制回来之后不能再改栏位了,改了还会出问题,但是可以改样式什么的,这个问题不一定会出现,出现了的话这个就是解决方法,
0 评论