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 map= battTinfService.getMonStatistic(stic); List bytes = new ArrayList(); //处理图片 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 goodList=(List) map.get("goodlist"); //创建优秀单体数据表 createMonSicTable(wb, goodList,"优秀单体数据表"); List badList=(List) map.get("badlist"); //创建劣化单体数据表 createMonSicTable(wb, badList,"劣化单体数据表"); List damageList=(List) 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 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=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 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=27; //插入图片 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.DONT_MOVE_AND_RESIZE); patriarch.createPicture(anchor, wb.addPicture(bytes.get(picNum), HSSFWorkbook.PICTURE_TYPE_PNG)).resize(1); picNum++; rowNum += rowNumAdd; } rowNum++; } }