yangys
2025-09-17 ee6af8dff591418168d5104d281a6f0bf3c961e8
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
package org.springblade.mdm.statreport.utils;
 
import org.apache.poi.xwpf.usermodel.*;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class XmlToDocxWithPOITest {
    private String checked = "☑";
    private String unchecked = "□";
    //@Test
    public void test() throws Exception {
 
        convert();
    }
 
    public void convert() {
        try {
            // 读取 Word 模板文件
            FileInputStream fis = new FileInputStream("d:/spd.docx");
            XWPFDocument document = new XWPFDocument(fis);
 
            // 准备要替换的数据
            Map<String, String> data = new HashMap<>();
            data.put("processNo","bh01");
            data.put("bz",checked);
            data.put("jd",unchecked);
            data.put("fzy",checked);
            data.put("fzn",unchecked);
 
            // 替换文档中的占位符
            replacePlaceholders(document, data);
 
            // 保存为新的 Word 文档
            FileOutputStream fos = new FileOutputStream("d:/poioutput.docx");
            document.write(fos);
            fos.close();
            fis.close();
 
            System.out.println("新的 Word 文档生成成功!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("生成新的 Word 文档失败:" + e.getMessage());
        }
    }
 
    private void replacePlaceholders(XWPFDocument document, Map<String, String> data) {
        // 遍历文档中的每个段落
        document.getTables().forEach(table -> {
            List<XWPFTableRow> rows = table.getRows();
            //遍历表格,并替换模板
            eachTable(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 (text.contains(placeholder)) {
                            text = text.replace(placeholder, replacement);
                            run.setText(text, 0);
                        }
                    }
                }
            }
        }
    }
 
    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 (text.contains(placeholder)) {
                        text = text.replace(placeholder, replacement);
                        run.setText(text, 0);
                    }
                }
            }
        }
    }
 
    public static void eachTable(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 (text.contains(placeholder)) {
                                        text = text.replace(placeholder, replacement);
                                        run.setText(text, 0);
                                    }
                                }
                            }
                        }
                    }
 
            }
        }
    }
}