李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
package com.chinaztt.mes.technology.util;
 
import com.chinaztt.mes.technology.dto.DocumentMaterialCostDTO;
import com.chinaztt.mes.technology.entity.RoutingOperationParam;
import lombok.SneakyThrows;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
 
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 *    工艺文件导出工具类
 * @author zhangxy
 */
public class DocumentWordExportUtils {
 
    private static final int BYTE_LEN = 100;
 
 
    /**
     * @param document 被写入的文件
     * @param style    word 模板的文字样式 从左往右  对应 从0开始
     * @param context  要写入的文字
     * @param bold     是否加粗
     * @param font     字体
     */
    public static void createText(XWPFDocument document, String style, String context, boolean bold, String font) {
        XWPFParagraph firstParagraph = document.createParagraph();
        firstParagraph.setAlignment(ParagraphAlignment.LEFT);
        if (style != null) {
            firstParagraph.setStyle(style);
        }
        XWPFRun run = firstParagraph.createRun();
 
        run.setText(context);
        run.setBold(bold);
        run.setFontFamily(font);
        //      run.addBreak();
    }
 
 
 
    /**
     * @param document
     * @param stringListMap map <规格 , 当前工序的参数的list>
     */
    public static void createParaTable(XWPFDocument document, Map<String, List<RoutingOperationParam>> stringListMap) {
        XWPFParagraph paragraph1 = document.createParagraph();
        XWPFRun paragraphRun1 = paragraph1.createRun();
        paragraphRun1.setText("\r");
 
        XWPFTable comTable = document.createTable();
        CTTblWidth comTableWidth = comTable.getCTTbl().addNewTblPr().addNewTblW();
        comTableWidth.setType(STTblWidth.DXA);
        comTableWidth.setW(BigInteger.valueOf(9072));
 
 
        //获取总列的数量
        int colTotal = stringListMap.keySet().size() * 2;
        XWPFTableRow infoTableRowOne = comTable.getRow(0);
        //获取总行数
        int rowTotal = 0;
        for (String key : stringListMap.keySet()) {
            if (stringListMap.get(key).size() > rowTotal) {
                rowTotal = stringListMap.get(key).size();
            }
        }
        rowTotal += 1;
        //画表格 先画第一行
        infoTableRowOne.getCell(0).setText("");
        for (int i = 1; i < colTotal; i++) {
            infoTableRowOne.addNewTableCell().setText("");
        }
        //再画第一列
        for (int i = 1; i < rowTotal; i++) {
            XWPFTableRow infoTableRow = comTable.createRow();
            infoTableRow.getCell(0).setText("");
        }
        //别的列和行
        for (int i = 1; i < colTotal; i++) {
            for (int j = 1; j < rowTotal; j++) {
                XWPFTableRow infoTableRow = comTable.getRow(j);
                infoTableRow.getCell(i).setText("");
            }
        }
        //当前行
        int col = 0;
        for (String key : stringListMap.keySet()) {
            infoTableRowOne.getCell(col).setText(key);
            infoTableRowOne.getCell(col + 1).setText("");
            List<RoutingOperationParam> processDocMaterialPageList = stringListMap.get(key);
            for (int j = 0; j < processDocMaterialPageList.size(); j++) {
                XWPFTableRow infoTableRow = comTable.getRow(j + 1);
                infoTableRow.getCell(col).setText(processDocMaterialPageList.get(j).getParameterItem());
                infoTableRow.getCell(col + 1).setText(processDocMaterialPageList.get(j).getParamValue());
            }
            col += 2;
        }
    }
 
 
    /**
     * @param document 被写入数据的文件
     * @param map      需要写入的数据 MAP形式
     * @param rows     每一行多少个
     */
    public static void createMeterialTable(XWPFDocument document, Map<String, List<DocumentMaterialCostDTO>>  map, int rows) {
        //这边为固定格式 不要动
        XWPFParagraph paragraph1 = document.createParagraph();
        XWPFRun paragraphRun1 = paragraph1.createRun();
        paragraphRun1.setText("\r");
 
        XWPFTable comTable = document.createTable();
        CTTblWidth comTableWidth = comTable.getCTTbl().addNewTblPr().addNewTblW();
        comTableWidth.setType(STTblWidth.DXA);
        comTableWidth.setW(BigInteger.valueOf(9072));
        //只有一张表的时候
        if (map.keySet().size() == 1) {
            for (String key : map.keySet()) {
                //这边开始处理数据
                List<DocumentMaterialCostDTO> list = map.get(key);
                //总数量 除以 行数 去除余数 先把整行数的 数据加进去 ,
                int total = (list.size() / rows);
                //获取第一行
                XWPFTableRow infoTableRowOne = comTable.getRow(0);
                //第一行第一格里面写入物料的规格
                infoTableRowOne.getCell(0).setText(key);
                //for 循环 卡 空格位置 相当与设置列
                for (int x = 1; x < rows; x++) {
                    infoTableRowOne.addNewTableCell().setText("");
                }
                //第二行往后
                for (int i = 0; i < total; i++) {
                    //处理物料名称行
                    XWPFTableRow infoTableRowTwo = comTable.createRow();
                    for (int y = 0; y < rows; y++) {
                        infoTableRowTwo.getCell(y).setText(list.get(i * rows + y).getPartName());
                    }
                    //处理物料数量行
                    XWPFTableRow infoTableRowThree = comTable.createRow();
                    for (int z = 0; z < rows; z++) {
                        infoTableRowThree.getCell(z).setText(list.get(i * rows + z).getQuantity() + "");
                    }
                }
                //这边开始处理取整后余下来的几条数据,最后会增加两行
                if (list.size() - total * rows != 0) {
                    XWPFTableRow infoTableRowTwo = comTable.createRow();
                    XWPFTableRow infoTableRowThree = comTable.createRow();
                    for (int i = 0; i < list.size() - total * rows; i++) {
                        infoTableRowTwo.getCell(i).setText(list.get(total * rows + i).getPartName());
                        infoTableRowThree.getCell(i).setText(list.get(total * rows + i).getQuantity() + "");
                    }
                }
            }
        } else {
            //获取总列的数量
            int colTotal = map.keySet().size() * 2;
            XWPFTableRow infoTableRowOne = comTable.getRow(0);
            //获取总行数
            int rowTotal = 0;
            for (String key : map.keySet()) {
                if (map.get(key).size() > rowTotal) {
                    rowTotal = map.get(key).size();
                }
            }
            rowTotal += 1;
            //画表格 先画第一行
            infoTableRowOne.getCell(0).setText("");
            for (int i = 1; i < colTotal; i++) {
                infoTableRowOne.addNewTableCell().setText("");
            }
            //再画第一列
            for (int i = 1; i < rowTotal; i++) {
                XWPFTableRow infoTableRow = comTable.createRow();
                infoTableRow.getCell(0).setText("");
            }
            //别的列和行
            for (int i = 1; i < colTotal; i++) {
                for (int j = 1; j < rowTotal; j++) {
                    XWPFTableRow infoTableRow = comTable.getRow(j);
                    infoTableRow.getCell(i).setText("");
                }
            }
            //当前行
            int col = 0;
            for (String key : map.keySet()) {
                infoTableRowOne.getCell(col).setText(key);
                infoTableRowOne.getCell(col + 1).setText("");
                List<DocumentMaterialCostDTO> processDocMaterialPageList = map.get(key);
                for (int j = 0; j < processDocMaterialPageList.size(); j++) {
                    XWPFTableRow infoTableRow = comTable.getRow(j + 1);
                    infoTableRow.getCell(col).setText(processDocMaterialPageList.get(j).getPartName());
                    infoTableRow.getCell(col + 1).setText(processDocMaterialPageList.get(j).getQuantity() + "");
                }
                col += 2;
            }
        }
    }
 
 
    public static XWPFParagraph newParagraph(XWPFParagraph paragraph) {
        XmlCursor xmlCursor = paragraph.getCTP().newCursor();
        xmlCursor.toNextSibling();
        paragraph = paragraph.getDocument().insertNewParagraph(xmlCursor);
        return paragraph;
    }
 
    /**
     * @param str        被处理的字符串
     * @param firstChar  识别的第一个字符
     * @param secondChar 识别的第二个字符
     * @param times      出现的次数
     * @return
     */
    public static String returnStation(String str, String firstChar, String secondChar, int times) {
        String result = "";
        try {
            Pattern pattern1 = Pattern.compile(firstChar);
            Matcher findMatcher1 = pattern1.matcher(str);
            Pattern pattern2 = Pattern.compile(secondChar);
            Matcher findMatcher2 = pattern2.matcher(str);
            int number = 0;
            int station1 = 0;
            int station2 = 0;
            while (findMatcher1.find()) {
                number++;
                if (number == times) {
                    break;
                }
            }
            station1 = findMatcher1.start();
            number = 0;
            while (findMatcher2.find()) {
                number++;
                if (number == times) {
                    break;
                }
            }
            station2 = findMatcher2.start();
            result = str.substring(station1 + 1, station2);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return result;
    }
 
 
    public static String[] returnStation(String str, String firstChar) {
        String[] sourceStrArray = str.split(firstChar);
        return sourceStrArray;
    }
 
    @SneakyThrows
    public static final byte[] input2byte(InputStream inStream) {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[BYTE_LEN];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, BYTE_LEN)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }
 
 
}