管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-02-10 014639637a80b0549cc0aee00b9a9670fb57827c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.lf.server.helper;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.xwpf.usermodel.*;
 
import java.io.*;
import java.util.List;
import java.util.Map;
 
/**
 * Word帮助类
 * @author WWW
 */
public class WordHelper {
    private final static Log log = LogFactory.getLog(WordHelper.class);
 
    /**
     * 通过word模板生成word的主方法
     */
    public static void generateWord(String inputFile, String outPutFile, Map<String, String> insertTextMap, List<String[]> addList) {
        try {
            FileInputStream inputStream = new FileInputStream(inputFile);
            FileOutputStream outputStream = new FileOutputStream(outPutFile);
 
            // 获取docx解析对象
            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
 
            // 处理所有文段数据,除了表格
            if (null != insertTextMap && insertTextMap.size() > 0) {
                handleParagraphs(xwpfDocument, insertTextMap);
            }
 
            // 处理表格数据
            handleTable(xwpfDocument, insertTextMap, addList);
 
            // 写入数据
            xwpfDocument.write(outputStream);
 
            outputStream.close();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 处理所有文段数据,除了表格
     */
    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> map, 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())) {
                if (null == map || map.isEmpty()) {
                    continue;
                }
 
                replaceData(rows, map);
            } else {
                insertData(table, addList);
            }
        }
    }
 
    /**
     * 替换数据
     */
    private static void replaceData(List<XWPFTableRow> rows, Map<String, String> map) {
        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(), map), 0);
                        }
                    }
                }
            }
        }
    }
 
    /**
     * 插入数据
     */
    private static void insertData(XWPFTable table,List<String[]> addList) {
        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("$");
    }
}