[测评系统]--测评系统核心代码库
wzp
2021-06-28 09ee32471c0b3adef524e386d5a464633a6a9437
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
package com.ots.common.utils.poi.consultation;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.util.Assert;
import java.util.List;
import java.util.Map;
import java.util.Optional;
 
public class PoiWordUtils {
    
    public static final String PREFIX_FIRST = "$";
    
    public static final String PREFIX_SECOND = "{";
    
    public static final String PLACEHOLDER_PREFIX = PREFIX_FIRST + PREFIX_SECOND;
    
    public static final String PLACEHOLDER_END = "}";
    
    public static final String addRowText = "tbAddRow:";
    public static final String addRowRepeatText = "tbAddRowRepeat:";
    
    public static final String addRowFlag = PLACEHOLDER_PREFIX + addRowText;
    
    public static final String addRowRepeatFlag = PLACEHOLDER_PREFIX + addRowRepeatText;
    
    public static final String tbRepeatMatrixSeparator = ",";
    
    public static final String PLACEHOLDER_SUFFIX = "}";
    
    public static final String PICTURE_PREFIX = PLACEHOLDER_PREFIX + "image:";
    
    public static boolean isAddRow(XWPFTableRow row) {
        return isDynRow(row, addRowFlag);
    }
    
    public static boolean isAddRowRepeat(XWPFTableRow row) {
        return isDynRow(row, addRowRepeatFlag);
    }
    private static boolean isDynRow(XWPFTableRow row, String dynFlag) {
        if (row == null) {
            return false;
        }
        List<XWPFTableCell> tableCells = row.getTableCells();
        if (tableCells != null) {
            XWPFTableCell cell = tableCells.get(0);
            if (cell != null) {
                String text = cell.getText();
                return text != null && text.startsWith(dynFlag);
            }
        }
        return false;
    }
    
    public static Object getValueByPlaceholder(Map<String, Object> paramMap, String key) {
        if (paramMap != null) {
            if (key != null) {
                return paramMap.get(getKeyFromPlaceholder(key));
            }
        }
        return null;
    }
    
    public static String getTbRepeatMatrix(String key) {
        Assert.notNull(key, "占位符为空");
        String $1 = key.replaceAll("\\" + PREFIX_FIRST + "\\" + PREFIX_SECOND + addRowRepeatText + "(.*:)(.*)" + "\\" + PLACEHOLDER_SUFFIX, "$2");
        return $1;
    }
    
    public static String getKeyFromPlaceholder(String placeholder) {
        return Optional.ofNullable(placeholder).map(p -> p.replaceAll("[\\$\\{\\}]", "")).get();
    }
    public static void main(String[] args) {
        String s = "${aa}";
        s = s.replaceAll(PLACEHOLDER_PREFIX + PLACEHOLDER_SUFFIX , "");
        System.out.println(s);
 
 
    }
    
    public static void copyCellAndSetValue(XWPFTableCell sourceCell, XWPFTableCell targetCell, String text) {
        
        List<XWPFParagraph> sourceCellParagraphs = sourceCell.getParagraphs();
        if (sourceCellParagraphs == null || sourceCellParagraphs.size() <= 0) {
            return;
        }
        XWPFParagraph sourcePar = sourceCellParagraphs.get(0);
        XWPFParagraph targetPar = targetCell.getParagraphs().get(0);
        
        targetPar.getCTP().setPPr(sourcePar.getCTP().getPPr());
        List<XWPFRun> sourceParRuns = sourcePar.getRuns();
        if (sourceParRuns != null && sourceParRuns.size() > 0) {
            
            List<XWPFRun> runs = targetPar.getRuns();
            Optional.ofNullable(runs).ifPresent(rs -> rs.stream().forEach(r -> r.setText("", 0)));
            if (runs != null && runs.size() > 0) {
                runs.get(0).setText(text, 0);
            } else {
                XWPFRun cellR = targetPar.createRun();
                cellR.setText(text, 0);
                
                targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
            }
            setTypeface(sourcePar, targetPar);
        } else {
            
            List<XWPFRun> runs = targetPar.getRuns();
            if (runs != null && runs.size() > 0) {
                runs.get(0).setText(text, 0);
            } else {
                XWPFRun newRun = targetPar.createRun();
                newRun.setText(text, 0);
            }
        }
    }
    
    private static void setTypeface(XWPFParagraph sourcePar, XWPFParagraph targetPar) {
        XWPFRun sourceRun = sourcePar.getRuns().get(0);
        String fontFamily = sourceRun.getFontFamily();
        
        String color = sourceRun.getColor();
 
        boolean bold = sourceRun.isBold();
        boolean italic = sourceRun.isItalic();
        int kerning = sourceRun.getKerning();
 
        UnderlinePatterns underline = sourceRun.getUnderline();
        XWPFRun targetRun = targetPar.getRuns().get(0);
        targetRun.setFontFamily(fontFamily);
 
        targetRun.setBold(bold);
        targetRun.setColor(color);
        targetRun.setItalic(italic);
        targetRun.setKerning(kerning);
        targetRun.setUnderline(underline);
        
    }
    
    public static boolean checkText(String text){
        boolean check  =  false;
        if(text.indexOf(PLACEHOLDER_PREFIX)!= -1){
            check = true;
        }
        return check;
    }
    
    public static String getPlaceholderReg(String text) {
        return "\\" + PREFIX_FIRST + "\\" + PREFIX_SECOND + text + "\\" + PLACEHOLDER_SUFFIX;
    }
    public static String getDocKey(String mapKey) {
        return PLACEHOLDER_PREFIX + mapKey + PLACEHOLDER_SUFFIX;
    }
    
    public static boolean isPicture(String text) {
        return text.startsWith(PICTURE_PREFIX);
    }
    
    public static void removeCells(XWPFTableRow row) {
        int size = row.getTableCells().size();
        try {
            for (int i = 0; i < size; i++) {
                row.removeCell(i);
            }
        } catch (Exception e) {
        }
    }
}