Excel多sheet多个Excel导出并且压缩成zip文件导出

 结合jeecg-boot框架,后端代码

    @AutoLog(value = "导出统计")
    @ApiOperation(value = "导出统计", notes = "导出统计")
    @PostMapping(value = "/exportXls")
    public String exportXls(@RequestBody PerformanceAppraisal performanceAppraisal, HttpServletRequest request, HttpServletResponse response) {
        byte[] content = new byte[0];
        int i = 1;
        String fileName = "绩效考核_" + LocalDateUtils.getLocalDateTimeUnSignedStr() + ".zip";
        try {
            String filePath = upLoadPath + "/achievements";
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            try (FileOutputStream outputStream = new FileOutputStream(filePath + "/" + fileName);
                 CheckedOutputStream checkedOutputStream = new CheckedOutputStream(outputStream, new Adler32());
                 ZipOutputStream zos = new ZipOutputStream(checkedOutputStream)) {

                LambdaQueryWrapper<PerformanceAppraisal> stationid = Wrappers.lambdaQuery();
                stationid.eq(PerformanceAppraisal::getStationId, performanceAppraisal.getStationId());
                stationid.eq(PerformanceAppraisal::getFillingMonth,performanceAppraisal.getFillingMonth());
                List<PerformanceAppraisal> station = performanceAppraisalService.list(stationid);
                for (PerformanceAppraisal s : station) {
                    List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
                    LambdaQueryWrapper<PerformanceAppraisal> wrapper = Wrappers.lambdaQuery();
                    wrapper.eq(PerformanceAppraisal::getId, s.getId());
                    List<PerformanceAppraisal> list = performanceAppraisalService.list(wrapper);

                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("title", getExportParams("效绩考核统计"));
                    map.put("entity", PerformanceAppraisal.class);
                    map.put("data", list);

                    listMap.add(map);

                    //多个map,对应了多个sheet
                    LambdaQueryWrapper<VisitSituation> wrapper1 = Wrappers.lambdaQuery();
                    wrapper1.eq(VisitSituation::getSpaId, s.getId());
                    List<VisitSituation> list1 = visitSituationService.list(wrapper1);

                    Map<String, Object> map1 = new HashMap<String, Object>();
                    map1.put("title", getExportParams("拜访情况"));
                    map1.put("entity", VisitSituation.class);
                    map1.put("data", list1);

                    listMap.add(map1);

                    //多个map,对应了多个sheet
                    LambdaQueryWrapper<EnterpriseRegistration> wrapper2 = Wrappers.lambdaQuery();
                    wrapper2.eq(EnterpriseRegistration::getSpaId, s.getId());
                    List<EnterpriseRegistration> list2 = enterpriseRegistrationService.list(wrapper2);

                    Map<String, Object> map2 = new HashMap<String, Object>();
                    map2.put("title", getExportParams("企业注册情况"));
                    map2.put("entity", EnterpriseRegistration.class);
                    map2.put("data", list2);

                    listMap.add(map2);

                    //多个map,对应了多个sheet
                    LambdaQueryWrapper<EnterpriseSettlement> wrapper3 = Wrappers.lambdaQuery();
                    wrapper3.eq(EnterpriseSettlement::getSpaId, s.getId());
                    List<EnterpriseSettlement> list3 = enterpriseSettlementService.list(wrapper3);

                    Map<String, Object> map3 = new HashMap<String, Object>();
                    map3.put("title", getExportParams("企业入驻情况"));
                    map3.put("entity", EnterpriseSettlement.class);
                    map3.put("data", list3);

                    listMap.add(map3);

                    Workbook wb = ExcelExportUtil.exportExcel(listMap, ExcelType.XSSF);
                    try (ByteArrayOutputStream os = new ByteArrayOutputStream();) {
                        wb.write(os);
                        content = os.toByteArray();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    zos.putNextEntry(new ZipEntry("绩效" + s.getCreateBy()+s.getFillingMonth() + ".xls"));
                    try (InputStream is = new ByteArrayInputStream(content);) {
                        int bytesRead = 0;
                        while ((bytesRead = is.read()) != -1) {
                            zos.write(bytesRead);
                        }
                        zos.closeEntry();
                    } catch (Exception e) {
                        log.info(e.getMessage());
                    }
                }
            } catch (Exception e) {
                log.info(e.getMessage());
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return fileName;
    }

    //导出参数
    public static ExportParams getExportParams(String name) {
        //表格名称,sheet名称,导出版本
        return new ExportParams(name, name, ExcelType.XSSF);
    }
 //在yml文件中配置
 // @Value("${hh.path.upload}")
 // private String upLoadPath;

前端代码(ant-vue)

handleOk(){
      const that = this;
      this.$refs.form.validate(valid => {
        if (valid) {
          let param = that.getQueryParams()
          downloadZip(param).then((data) => {
            let curPath = window.document.location.href;
            let ip = curPath.substring(0, curPath.indexOf(window.document.location.pathname));
            let filePath = ip + window._CONFIG['staticDomainURL'] + "/achievements/" + data
            window.location.href = filePath;
            that.close();
          })
        }
      })
    },

猜你喜欢

转载自blog.csdn.net/qq_60547244/article/details/123221524