whyclxw
2024-07-30 ed04733e15a5304c68ac16c121fd2989c851a57b
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package com.whyc.service;
 
import com.whyc.dto.paramter.POIWord;
import com.whyc.dto.paramter.TestDataInfDoc;
import com.whyc.mapper.BattInfMapper;
import com.whyc.pojo.BatttestdataId;
import com.whyc.util.ActionUtil;
import com.whyc.util.ThreadLocalUtil;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class PoIWordExportService {
    @Autowired(required = false)
    private BattInfMapper binfMapper;
    @Autowired
    private BatttestdataService bdataService;
 
    //word导出
    public void export(HttpServletRequest req, HttpServletResponse res) {
        String poiJson = req.getParameter("json");
        POIWord poiWord = ActionUtil.getGson().fromJson(poiJson, POIWord.class);
        List<TestDataInfDoc> list = bdataService.searchWordExport(poiWord);
        //String date = ActionUtil.sdfwithtime_yyyyMMdd_HH_mm_ss.format(new Date());
        String date = ThreadLocalUtil.format(new Date(),6);
        String fileName = "doc_" + date + "文件";
        XWPFDocument doc = new XWPFDocument();
        //标题
        createText(doc, "蓄电池放电数据报表", ParagraphAlignment.CENTER, 20);
        //使用单位
        createText(doc, "使用单位:" + poiWord.getUserName(), ParagraphAlignment.LEFT, 15);
        createText(doc, "产品名称:" + poiWord.getProdcutName(), ParagraphAlignment.LEFT, 15);
        createText(doc, "测试单位:" + poiWord.getTestDepart(), ParagraphAlignment.LEFT, 15);
        createText(doc, "测试人员:" + poiWord.getTestPeople(), ParagraphAlignment.LEFT, 15);
        createText(doc, "测试日期:" + poiWord.getTestDate(), ParagraphAlignment.LEFT, 15);
        createText(doc, "打印日期:" + poiWord.getPrintDate(), ParagraphAlignment.LEFT, 15);
        createText(doc, "测试站点:" + poiWord.getTestStationName(), ParagraphAlignment.LEFT, 15);
        //电池参数
        createText(doc, "电池参数:", ParagraphAlignment.LEFT, 15);
        createParam(doc, poiWord);
        //全部数据
        createText(doc, "全部数据:", ParagraphAlignment.LEFT, 15);
        createData(doc, list, poiWord.getMonCount());
        //结论表
        createText(doc, "结论表:", ParagraphAlignment.LEFT, 15);
        createResult(doc, poiWord);
        try {
            res.setCharacterEncoding("utf-8");
            res.setContentType("application/msword");
            res.setHeader("Access-Control-Expose-Headers", "Content-disposition");
            res.setHeader("Content-disposition", "attachment; filename=" +
                    URLEncoder.encode(fileName.concat(".doc"), "UTF-8"));
            OutputStream out = res.getOutputStream();
            doc.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
       /* try {
            String path = "D:\\test.doc";
            OutputStream os = new FileOutputStream(path);
            doc.write(os);
            if(os!=null){
                try{
                    os.close();
                    System.out.println("文件已输出!");
                }
                catch(IOException e){
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/
 
    }
 
    //结论表
    private void createResult(XWPFDocument doc, POIWord poiWord) {
        //添加表格
        XWPFTable table = doc.createTable(3, 4);
        table.setCellMargins(3, 5, 3, 5);
        XWPFTableRow row;
        XWPFTableCell cell;
        XWPFParagraph para;
        XWPFRun run;
        CTTcPr cellPr;
        Map<String, Map<String, String>> map = new HashMap<>();
        //第一行数据
        Map<String, String> rowMap1 = new HashMap<>();
        rowMap1.put("l1", "蓄电池初充电、放电、容量试验记录是否合格");
        rowMap1.put("l2", "蓄电池初充电、放电、容量试验记录是否合格");
        rowMap1.put("l3", "蓄电池初充电、放电、容量试验记录是否合格");
        rowMap1.put("l4", "" + poiWord.getTestIsGood());
        map.put("r1", rowMap1);
        //第二行数据
        Map<String, String> rowMap2 = new HashMap<>();
        rowMap2.put("l1", "负责人");
        rowMap2.put("l2", "" + poiWord.getHeadName());
        rowMap2.put("l3", "成员");
        rowMap2.put("l4", "" + poiWord.getGroupPeoples());
        map.put("r2", rowMap2);
        //第三行数据
        Map<String, String> rowMap3 = new HashMap<>();
        rowMap3.put("l1", "备注");
        rowMap3.put("l2", "" + poiWord.getNote());
        rowMap3.put("l3", "");
        rowMap3.put("l4", "");
        map.put("r3", rowMap3);
        for (int i = 0; i < 3; i++) {
            row = table.getRow(i);
            row.setHeight(400);
            for (int j = 0; j < 4; j++) {
                cell = row.getCell(j);
                cellPr = cell.getCTTc().addNewTcPr();
                cellPr.addNewTcW().setW(BigInteger.valueOf(3000));
                para = cell.getParagraphs().get(0);
                para.setAlignment(ParagraphAlignment.CENTER);
                run = para.createRun();
                run.setFontFamily("仿宋");
                run.setFontSize(11);
                String text = map.get("r" + (i + 1)).get("l" + (j + 1));
                run.setText(text);
            }
        }
        //合并单元格
        //2.列合并3行5,6,456789行的234
        mergeCellsHorizontal(table, 0, 0, 2);
        mergeCellsHorizontal(table, 2, 1, 3);
    }
 
    //全部数据
    private void createData(XWPFDocument doc, List<TestDataInfDoc> list, int monCount) {
        Map<String, Map<String, Object>> map = new HashMap<>();
        int lineCount = list.size();
        int rowCount = monCount + 4;
        //添加表格
        XWPFTable table = doc.createTable(rowCount, list.size() + 1);
        table.setCellMargins(3, 5, 3, 5);
        XWPFTableRow row;
        XWPFTableCell cell;
        XWPFParagraph para;
        XWPFRun run;
        CTTcPr cellPr;
        for (int i = 0; i < rowCount; i++) {
            Map<String, Object> mapRow = new HashMap<>();
            if (i == 0) {
                mapRow.put("l1", "放电时间");
                for (int j = 0; j < lineCount; j++) {
                    TestDataInfDoc infDoc = list.get(j);
                    mapRow.put("l" + (j + 2), ActionUtil.secToTime(infDoc.getTestTimelong()));
                }
            } else if (i == 1) {
                mapRow.put("l1", "总电压(V)");
                for (int j = 0; j < lineCount; j++) {
                    TestDataInfDoc infDoc = list.get(j);
                    mapRow.put("l" + (j + 2), infDoc.getGroupVol());
                }
            } else if (i == 2) {
                mapRow.put("l1", "电流(A)");
                for (int j = 0; j < lineCount; j++) {
                    TestDataInfDoc infDoc = list.get(j);
                    mapRow.put("l" + (j + 2), infDoc.getTestCurr());
                }
            } else if (i == 3) {
                mapRow.put("l1", "容量(AH)");
                for (int j = 0; j < lineCount; j++) {
                    TestDataInfDoc infDoc = list.get(j);
                    mapRow.put("l" + (j + 2), infDoc.getTestCap());
                }
            } else {
                int monNum = (i - 3);
                mapRow.put("l1", monNum + "#");
                for (int j = 0; j < lineCount; j++) {
                    TestDataInfDoc infDoc = list.get(j);
                    List<BatttestdataId> listData = infDoc.getDatalist();
                    for (int m = 0; m < listData.size(); m++) {
                        BatttestdataId bdataId = listData.get(m);
                        if (monNum == bdataId.getMonNum()) {
                            mapRow.put("l" + (j + 2), bdataId.getMonVol());
                        }
                    }
                }
            }
            map.put("r" + (i + 1), mapRow);
        }
        for (int i = 0; i < monCount + 4; i++) {
            row = table.getRow(i);
            row.setHeight(400);
            for (int j = 0; j < list.size() + 1; j++) {
                cell = row.getCell(j);
                cellPr = cell.getCTTc().addNewTcPr();
                cellPr.addNewTcW().setW(BigInteger.valueOf(3000));
                para = cell.getParagraphs().get(0);
                para.setAlignment(ParagraphAlignment.CENTER);
                run = para.createRun();
                run.setFontFamily("仿宋");
                run.setFontSize(11);
                String text = map.get("r" + (i + 1)).get("l" + (j + 1)).toString();
                run.setText(text);
            }
        }
    }
 
    //电池参数
    private void createParam(XWPFDocument doc, POIWord poiWord) {
        //添加表格
        XWPFTable table = doc.createTable(9, 5);
        table.setCellMargins(3, 5, 3, 5);
        XWPFTableRow row;
        XWPFTableCell cell;
        XWPFParagraph para;
        XWPFRun run;
        CTTcPr cellPr;
        Map<String, Map<String, String>> map = new HashMap<>();
        //第一行数据
        Map<String, String> rowMap1 = new HashMap<>();
        rowMap1.put("l1", "参数");
        rowMap1.put("l2", "型号");
        rowMap1.put("l3", "" + poiWord.getBattModel());
        rowMap1.put("l4", "制造厂");
        rowMap1.put("l5", "" + poiWord.getBattProducer());
        map.put("r1", rowMap1);
        //第二行数据
        Map<String, String> rowMap2 = new HashMap<>();
        rowMap2.put("l1", "参数");
        rowMap2.put("l2", "安装日期");
        rowMap2.put("l3", "" + poiWord.getBattInuseDate());
        rowMap2.put("l4", "检查日期");
        rowMap2.put("l5", "" + poiWord.getBattCheckDate());
        map.put("r2", rowMap2);
        //第三行数据
        Map<String, String> rowMap3 = new HashMap<>();
        rowMap3.put("l1", "参数");
        rowMap3.put("l2", "站名");
        rowMap3.put("l3", "" + poiWord.getTestStationName());
        rowMap3.put("l4", "组别");
        rowMap3.put("l5", "" + poiWord.getGroupName());
        map.put("r3", rowMap3);
        //第四行数据
        Map<String, String> rowmapRow = new HashMap<>();
        rowmapRow.put("l1", "外观检查");
        rowmapRow.put("l2", "(1)外壳是否有裂纹、损伤和漏液现象");
        rowmapRow.put("l3", "");
        rowmapRow.put("l4", "");
        rowmapRow.put("l5", "" + poiWord.getParamCheck1());
        map.put("r4", rowmapRow);
        //第五行数据
        Map<String, String> rowMap5 = new HashMap<>();
        rowMap5.put("l1", "外观检查");
        rowMap5.put("l2", "(2)正、负极性是否正确");
        rowMap5.put("l3", "");
        rowMap5.put("l4", "");
        rowMap5.put("l5", "" + poiWord.getParamCheck2());
        map.put("r5", rowMap5);
        //第六行数据
        Map<String, String> rowMap6 = new HashMap<>();
        rowMap6.put("l1", "外观检查");
        rowMap6.put("l2", "(3)环境温度C");
        rowMap6.put("l3", "");
        rowMap6.put("l4", "");
        rowMap6.put("l5", "" + poiWord.getParamCheck3());
        map.put("r6", rowMap6);
        //第七行数据
        Map<String, String> rowMap7 = new HashMap<>();
        rowMap7.put("l1", "外观检查");
        rowMap7.put("l2", "(4)检查连接条、螺丝是否紧固、锈蚀");
        rowMap7.put("l3", "");
        rowMap7.put("l4", "");
        rowMap7.put("l5", "" + poiWord.getParamCheck4());
        map.put("r7", rowMap7);
        //第八行数据
        Map<String, String> rowMap8 = new HashMap<>();
        rowMap8.put("l1", "记录");
        rowMap8.put("l2", "装置画面显示蓄电池电压值(V)");
        rowMap8.put("l3", "");
        rowMap8.put("l4", "");
        rowMap8.put("l5", "" + poiWord.getParamRecordVol1());
        map.put("r8", rowMap8);
        //第九行数据
        Map<String, String> rowMap9 = new HashMap<>();
        rowMap9.put("l1", "记录");
        rowMap9.put("l2", "实测蓄电池电压值(V)");
        rowMap9.put("l3", "");
        rowMap9.put("l4", "");
        rowMap9.put("l5", "" + poiWord.getParamRecordVol2());
        map.put("r9", rowMap9);
        for (int i = 0; i < 9; i++) {
            row = table.getRow(i);
            row.setHeight(400);
            for (int j = 0; j < 5; j++) {
                cell = row.getCell(j);
                cellPr = cell.getCTTc().addNewTcPr();
                cellPr.addNewTcW().setW(BigInteger.valueOf(3000));
                para = cell.getParagraphs().get(0);
                para.setAlignment(ParagraphAlignment.CENTER);
                run = para.createRun();
                run.setFontFamily("仿宋");
                run.setFontSize(11);
                String text = map.get("r" + (i + 1)).get("l" + (j + 1));
                run.setText(text);
            }
        }
        //合并单元格
        //1.行合并 1~3合并,4~7,8~9
        mergeCellsVertically(table, 0, 0, 2);
        mergeCellsVertically(table, 0, 3, 6);
        mergeCellsVertically(table, 0, 7, 8);
        //2.列合并3行5,6,456789行的234
        mergeCellsHorizontal(table, 3, 1, 3);
        mergeCellsHorizontal(table, 4, 1, 3);
        mergeCellsHorizontal(table, 5, 1, 3);
        mergeCellsHorizontal(table, 6, 1, 3);
        mergeCellsHorizontal(table, 7, 1, 3);
        mergeCellsHorizontal(table, 8, 1, 3);
    }
 
    //创建文本
    private void createText(XWPFDocument doc, String msg, ParagraphAlignment align, int fontSize) {
        XWPFParagraph para = doc.createParagraph();
        para.setAlignment(align);//设置左对齐
        XWPFRun run = para.createRun();
        run.setFontFamily("仿宋");
        run.setFontSize(fontSize);
        run.setText(msg);
        doc.createParagraph();
    }
 
    /**
     * @Description: 跨列合并
     */
    private void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
            XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
            if (cellIndex == fromCell) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }
 
    /**
     * @Description: 跨行合并
     * @seehttp://stackoverflow.com/questions/24907541/row-span-with-xwpftable
     */
    private void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
            if (rowIndex == fromRow) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }
    }
}