第一步。创建个map。里面是你要替换的唯一标识和对应的值
HashMap<String, String> map = new HashMap<>();
String filetype = legalInstrument.getFiletype();
map.put("${s1}", "把${s1}替换成指定内容。比如姓名啥的");
第二步。读取word模板。我是放到了resources下的templates下面
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("templates/CA签名模板.docx");
第三步。使用poi操作word
XWPFDocument doc = new XWPFDocument(inputStream);
POIWordUtil.replaceWord(doc, map);
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import cn.hutool.core.util.StrUtil;
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;
/**
* poi word工具类
*/
public class POIWordUtil {
/**
* 替换内容
*
* @param textMap 需要替换的信息集合
*/
public static void replaceWord(XWPFDocument document,
Map<String, String> textMap) {
//解析替换文本段落对象
changeText(document, textMap);
//解析替换表格对象
changeTable(document, textMap);
}
/**
* 替换段落文本
*
* @param document docx解析对象
* @param textMap 需要替换的信息集合
*/
public static void changeText(XWPFDocument document, Map<String, String> textMap) {
//获取段落集合
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
//判断此段落时候需要进行替换
String text = paragraph.getText();
if (StrUtil.isNotBlank(text) && text.contains("${")) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
//替换模板原来位置
String textValue = changeValue(run.toString(), textMap);
run.setText(textValue, 0);
}
}
}
}
/**
* 替换表格对象方法
*
* @param document docx解析对象
* @param textMap 需要替换的信息集合
*/
public static void changeTable(XWPFDocument document, Map<String, String> textMap) {
//获取表格对象集合
List<XWPFTable> tables = document.getTables();
for (int i = 0; i < tables.size(); i++) {
//只处理行数大于等于2的表格,且不循环表头
XWPFTable table = tables.get(i);
if (table.getRows().size() > 1) {
//判断表格内容是否可以替换
String cellText = table.getText();
if (StrUtil.isNotBlank(cellText) && cellText.contains("${")){
List<XWPFTableRow> rows = table.getRows();
//遍历表格,并替换模板
eachTable(rows, textMap);
}
}
}
}
/**
* 遍历表格
*
* @param rows 表格行对象
* @param textMap 需要替换的信息集合
*/
public static void eachTable(List<XWPFTableRow> rows, Map<String, String> textMap) {
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
//判断单元格内容是否可以替换
String cellText = cell.getText();
if (StrUtil.isNotBlank(cellText) && cellText.contains("${")) {
List<XWPFParagraph> paragraphs = cell.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
run.setText(changeValue(run.toString(), textMap), 0);
}
}
}
}
}
}
/**
* 匹配传入信息集合与模板
*
* @param value 模板需要替换的区域
* @param textMap 传入信息集合
* @return 模板需要替换区域信息集合对应值
*/
public static String changeValue(String value, Map<String, String> textMap) {
Set<Entry<String, String>> textSets = textMap.entrySet();
for (Entry<String, String> textSet : textSets) {
//匹配模板与替换值 格式${key}
// String key = "${" + textSet.getKey() + "}";
String key = textSet.getKey();
if (value.indexOf(key) != -1) {
value = value.replace(key, textSet.getValue());
}
}
return value;
}
}
最后一步。把替换后的word保存下面
//输出文件
File file = new File("要保存的路径");
OutputStream outputStream = new FileOutputStream(file);
doc.write(outputStream);
outputStream.close();