package org.springblade.mdm.statreport.utils;
|
|
import org.apache.poi.xwpf.usermodel.*;
|
import java.io.*;
|
import java.util.List;
|
import java.util.Map;
|
|
public class WordReplaceUtil {
|
public static final String CHECKED = "☑";
|
public static final String UNCHECKED = "□";
|
|
|
public static void convert(Map<String, String> data,InputStream inputStream,OutputStream outputSteam ) {
|
try {
|
// 读取 Word 模板文件
|
//FileInputStream fis = new FileInputStream("d:/spd.docx");
|
XWPFDocument document = new XWPFDocument(inputStream);
|
|
// 替换文档中的占位符
|
replacePlaceholders(document, data);
|
|
// 保存为新的 Word 文档
|
//FileOutputStream fos = new FileOutputStream("d:/poioutput.docx");
|
document.write(outputSteam);
|
|
//System.out.println("新的 Word 文档生成成功!");
|
} catch (IOException e) {
|
e.printStackTrace();
|
//System.out.println("生成新的 Word 文档失败:" + e.getMessage());
|
}
|
}
|
|
private static void replacePlaceholders(XWPFDocument document, Map<String, String> data) {
|
// 遍历文档中的每个段落
|
document.getTables().forEach(table -> {
|
List<XWPFTableRow> rows = table.getRows();
|
//遍历表格,并替换模板
|
replaceTableRow(rows, data);
|
|
table.getBody().getParagraphs().forEach(paragraph -> {
|
replaceTextInPhoto(paragraph,data);
|
});
|
});
|
for (XWPFParagraph paragraph : document.getParagraphs()) {
|
// 遍历段落中的每个文本运行对象
|
for (XWPFRun run : paragraph.getRuns()) {
|
String text = run.getText(0);
|
if (text != null) {
|
// 遍历数据映射,替换占位符
|
for (Map.Entry<String, String> entry : data.entrySet()) {
|
String placeholder = entry.getKey();
|
String replacement = entry.getValue();
|
if(replacement==null) {
|
replacement = "";
|
}
|
if (text.contains(placeholder)) {
|
text = text.replace(placeholder, replacement);
|
run.setText(text, 0);
|
}
|
}
|
}
|
}
|
}
|
}
|
|
static void replaceTextInPhoto(XWPFParagraph paragraph,Map<String,String> data) {
|
for (XWPFRun run : paragraph.getRuns()) {
|
String text = run.getText(0);
|
if (text != null) {
|
// 遍历数据映射,替换占位符
|
for (Map.Entry<String, String> entry : data.entrySet()) {
|
String placeholder = entry.getKey();
|
String replacement = entry.getValue();
|
if(replacement==null) {
|
replacement = "";
|
}
|
if (text.contains(placeholder)) {
|
text = text.replace(placeholder, replacement);
|
run.setText(text, 0);
|
}
|
|
}
|
}
|
}
|
}
|
|
public static void replaceTableRow(List<XWPFTableRow> rows , Map<String, String> data){
|
for (XWPFTableRow row : rows) {
|
//得到表格每一行的所有表格
|
List<XWPFTableCell> cells = row.getTableCells();
|
for (XWPFTableCell cell : cells) {
|
//判断单元格是否需要替换
|
|
List<XWPFParagraph> paragraphs = cell.getParagraphs();
|
for (XWPFParagraph paragraph : paragraphs) {
|
List<XWPFRun> runs = paragraph.getRuns();
|
for (XWPFRun run : runs) {
|
String text = run.getText(0);
|
if (text != null) {
|
// 遍历数据映射,替换占位符
|
for (Map.Entry<String, String> entry : data.entrySet()) {
|
String placeholder = entry.getKey();
|
String replacement = entry.getValue();
|
if(replacement==null) {
|
replacement = "";
|
}
|
|
if (text.contains(placeholder)) {
|
text = text.replace(placeholder, replacement);
|
run.setText(text, 0);
|
}
|
|
}
|
}
|
}
|
}
|
|
}
|
}
|
}
|
}
|