管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-02-10 421ecefe1a6885727c327d0c1bb2f70d5bb91753
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
package com.lf.server.helper;
 
import org.apache.poi.xwpf.usermodel.*;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
 
/**
 * Word帮助类
 * @author WWW
 */
public class WordHelper {
    /**
     * 通过word模板生成word的主方法
     */
    public static void generateWord(InputStream inputStream, OutputStream outputStream, Map<String, String> insertTextMap, List<String[]> addList) throws IOException {
        // 获取docx解析对象
        XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
 
        // 处理所有文段数据,除了表格
        handleParagraphs(xwpfDocument, insertTextMap);
 
        // 处理表格数据
        handleTable(xwpfDocument, insertTextMap, addList);
 
        // 写入数据
        xwpfDocument.write(outputStream);
 
        outputStream.close();
    }
 
    /**
     * 处理所有文段数据,除了表格
     */
    public static void handleParagraphs(XWPFDocument xwpfDocument, Map<String, String> insertTextMap) {
        for (XWPFParagraph paragraph : xwpfDocument.getParagraphs()) {
            String text = paragraph.getText();
            if (isReplacement(text)) {
                for (XWPFRun run : paragraph.getRuns()) {
                    // 判断带有 ${} 的run
                    run.setText(matchesValue(run.text(), insertTextMap), 0);
                }
            }
        }
    }
 
    /**
     * 处理表格数据方法
     */
    public static void handleTable(XWPFDocument xwpfDocument, Map<String, String> insertTextMap, List<String[]> addList) {
        List<XWPFTable> tables = xwpfDocument.getTables();
        for (XWPFTable table : tables) {
            List<XWPFTableRow> rows = table.getRows();
            if (rows.size() < 2) {
                continue;
            }
 
            if (isReplacement(table.getText())) {
                // 替换数据
                for (XWPFTableRow row : rows) {
                    List<XWPFTableCell> tableCells = row.getTableCells();
                    for (XWPFTableCell tableCell : tableCells) {
                        if (isReplacement(tableCell.getText())) {
                            List<XWPFParagraph> paragraphs = tableCell.getParagraphs();
                            for (XWPFParagraph paragraph : paragraphs) {
                                List<XWPFRun> runs = paragraph.getRuns();
                                for (XWPFRun run : runs) {
                                    run.setText(matchesValue(tableCell.getText(), insertTextMap), 0);
                                }
                            }
                        }
                    }
                }
            } else {
                // 插入数据
                for (int i = 1; i < addList.size(); i++) {
                    XWPFTableRow row = table.createRow();
                }
 
                List<XWPFTableRow> rowList = table.getRows();
                for (int i = 1; i < rowList.size(); i++) {
                    XWPFTableRow xwpfTableRow = rowList.get(i);
                    List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
                    for (int j = 0; j < tableCells.size(); j++) {
                        XWPFTableCell xwpfTableCell = tableCells.get(j);
                        xwpfTableCell.setText(addList.get(i - 1)[j]);
                    }
                }
            }
        }
    }
 
    /**
     * 有${}的值匹配出替换的数据,没有${}就返回原来的数据
     *
     * @param wordValue ${...} 带${}的变量
     * @param map       存储需要替换的数据
     * @return java.lang.String
     */
    public static String matchesValue(String wordValue, Map<String, String> map) {
        for (String s : map.keySet()) {
            String s1 = "${" + s + "}";
            if (s1.equals(wordValue)) {
                wordValue = map.get(s);
            }
        }
 
        return wordValue;
    }
 
    /**
     * 测试是否包含需要替换的数据
     */
    public static boolean isReplacement(String text) {
        return text.contains("$");
    }
}