whyclxw
2025-06-05 bd74a6dc4c07a8fd827396ab7049bba3d7a7f378
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
package com.whyc.service;
 
import com.whyc.dto.Statistic.MonStic;
import com.whyc.dto.Statistic.SticMonRes;
import com.whyc.pojo.db_user.User;
import com.whyc.util.ActionUtil;
import com.whyc.util.ServletUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Service
public class ExportService {
 
    @Autowired
    private BatttestdataInfService battTinfService;
 
 
    //单节数量统计导出(1.2.14)
    public void exportBattTinfStatistic(HttpServletRequest req, HttpServletResponse resp, MonStic stic) throws ParseException {
        String topPic = stic.getTopPic();            //头部柱状图
        String goodVolPic = stic.getGoodVolPic();            //优秀电压图
        String goodResPic = stic.getGoodResPic();        //优秀内阻
        String badVolPic = stic.getBadVolPic();            //劣化电压图
        String badResPic = stic.getBadResPic();        //劣化内阻
        String damageVolPic = stic.getDamageVolPic();            //损坏电压图
        String damageResPic = stic.getDamageResPic();        //损坏内阻
        User uinf= ActionUtil.getUser();
        stic.setUid(uinf.getId());
        Map<String,Object> map= battTinfService.getMonStatistic(stic);
        List<byte[]> bytes = new ArrayList<byte[]>();
        //处理图片
        try {
            if (ServletUtils.isNotNull(topPic)) {
                String[] url = topPic.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
            if (ServletUtils.isNotNull(goodVolPic)) {
                String[] url = goodVolPic.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
            if (ServletUtils.isNotNull(goodResPic)) {
                String[] url = goodResPic.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
            if (ServletUtils.isNotNull(badVolPic)) {
                String[] url = badVolPic.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
            if (ServletUtils.isNotNull(badResPic)) {
                String[] url = badResPic.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
            if (ServletUtils.isNotNull(damageVolPic)) {
                String[] url = damageVolPic.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
            if (ServletUtils.isNotNull(damageResPic)) {
                String[] url = damageResPic.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        //当前日期
        String nowFormat = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String excelName = "单节数量统计-"+nowFormat;
 
        HSSFWorkbook wb = new HSSFWorkbook();
        //单体性能统计表
        createMonSicPic(wb, bytes);
        List<SticMonRes> goodList=(List<SticMonRes>) map.get("goodlist");
        //创建优秀单体数据表
        createMonSicTable(wb, goodList,"优秀单体数据表");
        List<SticMonRes> badList=(List<SticMonRes>) map.get("badlist");
        //创建劣化单体数据表
        createMonSicTable(wb, badList,"劣化单体数据表");
        List<SticMonRes> damageList=(List<SticMonRes>) map.get("damagelist");
        //创建损坏单体数据表
        createMonSicTable(wb, damageList,"损坏单体数据表");
 
        try {
            // 转码防止乱码
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode ( excelName+".xlsx", "utf-8"));
            OutputStream out = resp.getOutputStream();
            wb.write(out);
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    //创建优秀/劣化/损坏单体数据表
    private void createMonSicTable(HSSFWorkbook wb, List<SticMonRes> list,String sheetName) {
        HSSFSheet sheet = wb.createSheet(sheetName);
        sheet.setDefaultColumnWidth(15);    //设置默认列宽,实际上回多出2个字符,不知道为什么
        sheet.setDefaultRowHeight((short) (1 * 256));//设置默认行高,表示2个字符的高度
        int rownum = 1;
 
        String[] arrTh = new String[]{
                "机房名称","设备名称","电池组名称","单体编号","浮充电压(V)","单体内阻(mΩ)"
        };
        sheet.createRow(rownum);
        for(int i = 0 ;i<arrTh.length;i++){
            sheet.getRow(rownum).createCell(i+1).setCellValue(arrTh[i]);
        }
        rownum++;
        if(list!=null&&list.size()>=0){
            for(SticMonRes res:list){
                sheet.createRow(rownum);
                sheet.getRow(rownum).createCell(1).setCellValue(res.getStationName());
                sheet.getRow(rownum).createCell(2).setCellValue(res.getDevName());
                sheet.getRow(rownum).createCell(3).setCellValue(res.getBattgroupName());
                sheet.getRow(rownum).createCell(4).setCellValue(res.getMonNum());
                sheet.getRow(rownum).createCell(5).setCellValue(res.getMonVol());
                sheet.getRow(rownum).createCell(6).setCellValue(res.getMonRes());
                rownum++;
            }
        }
    }
 
    //单体性能统计表
    private void createMonSicPic(HSSFWorkbook wb, List<byte[]> bytes) {
 
        HSSFSheet sheet = wb.createSheet("单体性能统计表");
        sheet.setDefaultColumnWidth(15);    //设置默认列宽,实际上回多出2个字符,不知道为什么
        sheet.setDefaultRowHeight((short) (2 * 256)); //设置默认行高,表示2个字符的高度
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setBold(true);
        cellStyle.setFont(font);
 
        //图片元素
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        int rowNum = 1;
        int rowNumAdd=25;
        //插入图片
        int picNum = 0;
        String[] picName = new String[]{
                "单体性能统计图", "优秀单体电压图", "优秀单体内阻图", "劣化单体电压图", "劣化单体内阻图", "损坏单体电压图", "损坏单体内阻图"
        };
        for (int i = 0; i < picName.length; i++) {
            sheet.createRow(rowNum - 1);
            sheet.getRow(rowNum - 1).createCell(0).setCellValue(picName[i]);
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255, (short) 0, rowNum, (short) 10, rowNum + rowNumAdd);
            anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);
            patriarch.createPicture(anchor, wb.addPicture(bytes.get(picNum), HSSFWorkbook.PICTURE_TYPE_PNG)).resize(1);
            picNum++;
            rowNum = rowNum+rowNumAdd+5;
        }
    }
}