测试 用electron + springboot 构建桌面应用
whyczh
2022-03-19 3cae48fd8b3e0f2b2111b0424bb50a4b484c8cc4
更新
1个文件已修改
182 ■■■■ 已修改文件
src/main/java/testElectronJ/TestController.java 182 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/testElectronJ/TestController.java
@@ -22,6 +22,7 @@
public class TestController {
    //@Autowired
    //private FboDataInf fboDataInf;
    public static String fileUrl = "";
    @GetMapping("/hello")
    public Response hello(){
@@ -33,6 +34,7 @@
        FboDataInf fboDataInf = new FboDataInf();
        //String filePath1 = java.net.URLDecoder.decode(filePath,"utf-8");
        fboDataInf.readFboFile(filePath);
        TestController.fileUrl = filePath;
        return new Response().set(1,fboDataInf,filePath);
    }
@@ -42,28 +44,37 @@
        String curr_echart = req.getParameter("curr_echart");        //电池电流折线图
        String groupVol_echart = req.getParameter("groupVol_echart");        //组端电压折线图
        String cap_echart = req.getParameter("cap_echart");            //测试容量折线图
        String actualCap_echart = req.getParameter("actualCap_echart");            //实际容量折线图
        String vol_echart = req.getParameter("vol_echart");            //单体电压折线图
        String filePath = req.getParameter("filePath");
        FboDataInf fboDataInf = new FboDataInf();
        if (filePath==null || "".equals(filePath)){
            filePath = fileUrl;
        }
        fboDataInf.readFboFile(filePath);
        //从文件中获取数据
        List<FboData> list = fboDataInf.fboData;
        //图片base64后的数据
        List<byte[]> bytes = new ArrayList<>();
        try {
            if (ServletUtils.isNotNull(curr_echart)) {
                String[] url = curr_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(groupVol_echart)) {
            }
            if (ServletUtils.isNotNull(groupVol_echart)) {
                String[] url = groupVol_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(cap_echart)) {
            }
            if (ServletUtils.isNotNull(cap_echart)) {
                String[] url = cap_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(vol_echart)) {
            }
            if (ServletUtils.isNotNull(actualCap_echart)) {
                String[] url = actualCap_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
            if (ServletUtils.isNotNull(vol_echart)) {
                String[] url = vol_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
@@ -72,10 +83,10 @@
        }
        //当前日期
        String nowFormat = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String excelName = "FGCD-"+nowFormat;
        String excelName = "FBX-"+nowFormat;
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Sheet1");
        HSSFSheet sheet = wb.createSheet("图表");
        //图片元素
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
@@ -91,7 +102,7 @@
        //插入图片
        int picNum = 0;
        String[] picName = new String[]{
                "电池电流折线图","组端电压折线图","测试容量折线图","单体电压折线图"
                "电池电流折线图","组端电压折线图","测试容量折线图","实际容量折线图","单体电压折线图"
        };
        if (picName.length==bytes.size()){
            for(int i=0;i<picName.length;i++){
@@ -113,7 +124,7 @@
        int rowNum = 0;
        HSSFRow rowTitle = sheet1.createRow(rowNum);
        HSSFCell cellTitle = rowTitle.createCell(0);
        cellTitle.setCellValue("一体机充放电数据");
        cellTitle.setCellValue("测试数据");
        cellTitle.setCellStyle(cellStyle);
        rowNum++;
@@ -127,13 +138,17 @@
        //属性栏
        String[] rowName = {"测试时间","总电压","测试容量","剩余容量"};
        HSSFRow row = sheet1.createRow(rowNum);
        row.createCell(0).setCellValue("测试时间"); //创建单元格
        row.createCell(1).setCellValue("总电压");
        row.createCell(2).setCellValue("测试容量");
        row.createCell(3).setCellValue("剩余容量");
        for (int i=0;i<rowName.length;){
            row.createCell(i).setCellValue(rowName[i]);
        }
        //row.createCell(0).setCellValue("测试时间"); //创建单元格
        //row.createCell(1).setCellValue("总电压");
        //row.createCell(2).setCellValue("测试容量");
        //row.createCell(3).setCellValue("剩余容量");
        for(int i=0;i<list.get(0).SingleVol.length;i++){
            row.createCell(4+i).setCellValue("#"+Integer.valueOf(i+1));
            row.createCell(rowName.length+i).setCellValue("#"+Integer.valueOf(i+1));
        }
        rowNum++;
        //数据栏
@@ -149,7 +164,6 @@
                    sheet1.getRow(rowNum).createCell(4+k).setCellValue(fbo.SingleVol[k]);
                }
            }
            i=i+5-1;
            rowNum++;
        }
@@ -171,140 +185,6 @@
    }
    public void export1(HttpServletRequest req, HttpServletResponse resp){
        String curr_echart = req.getParameter("curr_echart");        //电池电流折线图
        String groupVol_echart = req.getParameter("groupVol_echart");        //组端电压折线图
        String cap_echart = req.getParameter("cap_echart");            //测试容量折线图
        String vol_echart = req.getParameter("vol_echart");            //单体电压折线图
        String filePath = req.getParameter("filePath");
        FboDataInf fboDataInf = new FboDataInf();
        fboDataInf.readFboFile(filePath);
        List<FboData> list = fboDataInf.fboData;
        List<byte[]> bytes = new ArrayList<>();
        try {
            if (ServletUtils.isNotNull(curr_echart)) {
                String[] url = curr_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(groupVol_echart)) {
                String[] url = groupVol_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(cap_echart)) {
                String[] url = cap_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(vol_echart)) {
                String[] url = vol_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //当前日期
        String nowFormat = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String excelName = "FGCD-"+nowFormat;
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Sheet1");
        //图片元素
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        int rowNumSheet = 0;
        //新建行
        //抬头
        int rowNum = 0;
        HSSFRow rowTitle = sheet.createRow(rowNum);
        HSSFCell cellTitle = rowTitle.createCell(0);
        cellTitle.setCellValue("一体机充放电数据");
        cellTitle.setCellStyle(cellStyle);
        rowNum++;
        FboDataHeadStart start = fboDataInf.fboDataStart;
        HSSFRow rowHand = sheet.createRow(rowNum);
        rowHand.createCell(0).setCellValue("电池名称:");
        rowHand.createCell(1).setCellValue(start.battNameStr);
        rowHand.createCell(2).setCellValue("电池品牌:");
        rowHand.createCell(3).setCellValue(start.battBrandStr);
        rowNum++;
        //属性栏
        HSSFRow row = sheet.createRow(rowNum);
        row.createCell(0).setCellValue("测试时间"); //创建单元格
        row.createCell(1).setCellValue("总电压");
        row.createCell(2).setCellValue("测试容量");
        row.createCell(3).setCellValue("剩余容量");
        for(int i=0;i<list.get(0).SingleVol.length;i++){
            row.createCell(4+i).setCellValue("#"+Integer.valueOf(i+1));
        }
        rowNum++;
        //数据栏
        for (int i = 0; i < list.size(); i++) {
            sheet.createRow(rowNum);  //创建行
            for(int j=0;j<5;j++){
                FboData fbo=list.get(i);
                sheet.getRow(rowNum).createCell(0).setCellValue(fbo.m_TestTime.hms());
                sheet.getRow(rowNum).createCell(1).setCellValue(fbo.SumVoltage);
                sheet.getRow(rowNum).createCell(2).setCellValue(fbo.testCap);
                sheet.getRow(rowNum).createCell(3).setCellValue(fbo.residualCap);
                for (int k = 0;k<list.get(0).SingleVol.length;k++){
                    sheet.getRow(rowNum).createCell(4+k).setCellValue(fbo.SingleVol[k]);
                }
            }
            i=i+5-1;
            rowNum++;
        }
        rowNum+=2;
        //插入图片
        int picNum = 0;
        String[] picName = new String[]{
                "电池电流折线图","组端电压折线图","测试容量折线图","单体电压折线图"
        };
        if (picName.length==bytes.size()){
            for(int i=0;i<picName.length;i++){
                sheet.createRow(rowNum-1);
                sheet.getRow(rowNum-1).createCell(0).setCellValue(picName[i]);
                //rowNum++;
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255,(short) 0, rowNum, (short) 10, rowNum+27);
                anchor.setAnchorType(3);
                patriarch.createPicture(anchor,wb.addPicture(bytes.get(picNum),HSSFWorkbook.PICTURE_TYPE_PNG)).resize(1);
                picNum ++;
                rowNum+=27;
            }
        }
        rowNum++;
        try {
            // 转码防止乱码
            resp.addHeader("Content-Disposition", "attachment;filename="
                    + new String(excelName.getBytes("UTF-8"), "ISO8859-1")
                    + ".xls");
            OutputStream out = resp.getOutputStream();
            wb.write(out);
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}