月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-17 796b44ea813a1133beae4f3a67f1c0263510c0c7
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.moon.server.helper;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
 
import java.io.*;
import java.util.List;
import java.util.Map;
 
@SuppressWarnings("ALL")
public class WordHelper {
    private final static Log log = LogFactory.getLog(WordHelper.class);
 
    public static void generateWord(String inputFile, String outPutFile, Map<String, String> insertTextMap, List<String[]> addList) {
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
 
        try {
            inputStream = new FileInputStream(inputFile);
            outputStream = new FileOutputStream(outPutFile);
 
            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
 
            if (null != insertTextMap && insertTextMap.size() > 0) {
                handleParagraphs(xwpfDocument, insertTextMap);
            }
 
            handleTable(xwpfDocument, insertTextMap, addList);
 
            xwpfDocument.write(outputStream);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
 
    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.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, c = addList.size(); i < c; i++) {
            table.createRow();
        }
 
        List<XWPFTableCell> oldCells = table.getRow(1).getTableCells();
        List<XWPFTableRow> rowList = table.getRows();
        for (int i = 0, c = addList.size(); i < c; i++) {
            XWPFTableRow xwpfTableRow = rowList.get(i + 1);
 
            List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
            for (int j = 0; j < tableCells.size(); j++) {
                XWPFTableCell oldCell = oldCells.get(j);
                XWPFTableCell newCell = tableCells.get(j);
 
                if (0 == i) {
                    // newCell.setText(addList.get(i)[j])
                    setCellText(newCell, addList.get(i)[j]);
                } else {
                    setCellText(oldCell, newCell, addList.get(i)[j]);
                }
            }
        }
    }
 
    private static void setCellText(XWPFTableCell cell, String text) {
        List<XWPFParagraph> paragraphs = cell.getParagraphs();
        for (XWPFParagraph paragraph : paragraphs) {
            List<XWPFRun> runs = paragraph.getRuns();
            for (XWPFRun run : runs) {
                run.setText(text, 0);
            }
        }
    }
 
    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("$");
    }
 
    private static void setCellText(XWPFTableCell tmpCell, XWPFTableCell cell, String text) {
        CTTc cttc2 = tmpCell.getCTTc();
        CTTcPr ctPr2 = cttc2.getTcPr();
        CTTc cttc = cell.getCTTc();
        CTTcPr ctPr = cttc.addNewTcPr();
        if (ctPr2.getTcW() != null) {
            ctPr.addNewTcW().setW(ctPr2.getTcW().getW());
        }
        if (ctPr2.getVAlign() != null) {
            ctPr.addNewVAlign().setVal(ctPr2.getVAlign().getVal());
        }
        if (ctPr2.getTcBorders() != null) {
            ctPr.setTcBorders(ctPr2.getTcBorders());
        }
 
        XWPFParagraph tmpP = tmpCell.getParagraphs().get(0);
        XWPFParagraph cellP = cell.getParagraphs().get(0);
        XWPFRun tmpR = null;
        if (tmpP.getRuns() != null && tmpP.getRuns().size() > 0) {
            tmpR = tmpP.getRuns().get(0);
        }
        XWPFRun cellR = cellP.createRun();
        cellR.setText(text);
 
        copyFontInfo(cellR, tmpR);
 
        cellP.setAlignment(tmpP.getAlignment());
        cellP.setVerticalAlignment(tmpP.getVerticalAlignment());
        cellP.setBorderBetween(tmpP.getBorderBetween());
        cellP.setBorderBottom(tmpP.getBorderBottom());
        cellP.setBorderLeft(tmpP.getBorderLeft());
        cellP.setBorderRight(tmpP.getBorderRight());
        cellP.setBorderTop(tmpP.getBorderTop());
        cellP.setPageBreak(tmpP.isPageBreak());
 
        if (tmpP.getCTP() != null&&tmpP.getCTP().getPPr() != null) {
            CTPPr tmpPpr = tmpP.getCTP().getPPr();
            CTPPr cellPpr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr();
 
            copySpacing(tmpPpr, cellPpr);
            copyParagraph(tmpPpr, cellPpr);
        }
    }
 
    private static void copyFontInfo(XWPFRun cellR, XWPFRun tmpR) {
        if (tmpR == null) {
            return;
        }
        if (!cellR.isBold()) {
            cellR.setBold(tmpR.isBold());
        }
        cellR.setItalic(tmpR.isItalic());
        cellR.setUnderline(tmpR.getUnderline());
        cellR.setColor(tmpR.getColor());
        cellR.setTextPosition(tmpR.getTextPosition());
        if (tmpR.getFontSize() != -1) {
            cellR.setFontSize(tmpR.getFontSize());
        }
        if (tmpR.getFontFamily() != null) {
            cellR.setFontFamily(tmpR.getFontFamily());
        }
        if (tmpR.getCTR() != null) {
            if (tmpR.getCTR().isSetRPr()) {
                CTRPr tmpRpr = tmpR.getCTR().getRPr();
                if (tmpRpr.isSetRFonts()) {
                    CTFonts tmpFonts = tmpRpr.getRFonts();
                    CTRPr cellRpr = cellR.getCTR().isSetRPr() ? cellR
                            .getCTR().getRPr() : cellR.getCTR().addNewRPr();
                    CTFonts cellFonts = cellRpr.isSetRFonts() ? cellRpr
                            .getRFonts() : cellRpr.addNewRFonts();
                    cellFonts.setAscii(tmpFonts.getAscii());
                    cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme());
                    cellFonts.setCs(tmpFonts.getCs());
                    cellFonts.setCstheme(tmpFonts.getCstheme());
                    cellFonts.setEastAsia(tmpFonts.getEastAsia());
                    cellFonts.setEastAsiaTheme(tmpFonts.getEastAsiaTheme());
                    cellFonts.setHAnsi(tmpFonts.getHAnsi());
                    cellFonts.setHAnsiTheme(tmpFonts.getHAnsiTheme());
                }
            }
        }
    }
 
    private static void copySpacing(CTPPr tmpPpr, CTPPr cellPpr) {
        CTSpacing tmpSpacing = tmpPpr.getSpacing();
        if (tmpSpacing != null) {
            CTSpacing cellSpacing = cellPpr.getSpacing() != null ? cellPpr.getSpacing() : cellPpr.addNewSpacing();
            if (tmpSpacing.getAfter() != null) {
                cellSpacing.setAfter(tmpSpacing.getAfter());
            }
            if (tmpSpacing.getAfterAutospacing() != null) {
                cellSpacing.setAfterAutospacing(tmpSpacing
                        .getAfterAutospacing());
            }
            if (tmpSpacing.getAfterLines() != null) {
                cellSpacing.setAfterLines(tmpSpacing.getAfterLines());
            }
            if (tmpSpacing.getBefore() != null) {
                cellSpacing.setBefore(tmpSpacing.getBefore());
            }
            if (tmpSpacing.getBeforeAutospacing() != null) {
                cellSpacing.setBeforeAutospacing(tmpSpacing
                        .getBeforeAutospacing());
            }
            if (tmpSpacing.getBeforeLines() != null) {
                cellSpacing.setBeforeLines(tmpSpacing.getBeforeLines());
            }
            if (tmpSpacing.getLine() != null) {
                cellSpacing.setLine(tmpSpacing.getLine());
            }
            if (tmpSpacing.getLineRule() != null) {
                cellSpacing.setLineRule(tmpSpacing.getLineRule());
            }
        }
    }
 
    private static void copyParagraph(CTPPr tmpPpr, CTPPr cellPpr) {
        CTInd tmpInd = tmpPpr.getInd();
        if (tmpInd != null) {
            CTInd cellInd = cellPpr.getInd() != null ? cellPpr.getInd() : cellPpr.addNewInd();
            if (tmpInd.getFirstLine() != null) {
                cellInd.setFirstLine(tmpInd.getFirstLine());
            }
            if (tmpInd.getFirstLineChars() != null) {
                cellInd.setFirstLineChars(tmpInd.getFirstLineChars());
            }
            if (tmpInd.getHanging() != null) {
                cellInd.setHanging(tmpInd.getHanging());
            }
            if (tmpInd.getHangingChars() != null) {
                cellInd.setHangingChars(tmpInd.getHangingChars());
            }
            if (tmpInd.getLeft() != null) {
                cellInd.setLeft(tmpInd.getLeft());
            }
            if (tmpInd.getLeftChars() != null) {
                cellInd.setLeftChars(tmpInd.getLeftChars());
            }
            if (tmpInd.getRight() != null) {
                cellInd.setRight(tmpInd.getRight());
            }
            if (tmpInd.getRightChars() != null) {
                cellInd.setRightChars(tmpInd.getRightChars());
            }
        }
    }
}