easyExcel导出多sheet表格,最简单快捷写法

 /**
     * 导出
     */
    @PostMapping("/export")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "导出", notes = "传入ids")
    public void export(HttpServletResponse response) throws IOException {

	
        ArrayList batchExcelList = new ArrayList<>();
        ArrayList freshmanExcelList = new ArrayList<>();
        ArrayList testResultExcelArrayList = new ArrayList<>();

        //查询所有的batch
        List batchList = batchService.list();
        List freshmanList = freshmanService.list(new QueryWrapper().lambda().last("limit 1000"));
        List testResults = testResultService.list(new QueryWrapper().lambda().last("limit 1000"));

        for (TestResult testResult : testResults) {
            TestResultExcel testResultExcel = new TestResultExcel();
            BeanUtil.copyProperties(testResult, testResultExcel);
            testResultExcelArrayList.add(testResultExcel);
        }

        for (Batch batch : batchList) {
            BatchExcel batchExcel = new BatchExcel();
            BeanUtil.copyProperties(batch, batchExcel);
            //将开始日期和结束日期和通知时间转换成字符串
//			batchExcel.setStartDate(batch.getStartDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            batchExcel.setStartDate(batch.getStartDate().toString());
            batchExcel.setEndDate(batch.getEndDate().toString());
            batchExcel.setNoticeTime(batch.getNoticeTime().toString());
            batchExcelList.add(batchExcel);
        }
        for (Freshman freshman : freshmanList) {
            FreshmanExcel freshmanExcel = new FreshmanExcel();
            BeanUtil.copyProperties(freshman, freshmanExcel);
            freshmanExcelList.add(freshmanExcel);
        }


        String file = "xrrj.zip";
        response.setCharacterEncoding("utf-8");
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file, "UTF-8"));

        ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        try {
            if (!CollectionUtils.isEmpty(batchExcelList) || !CollectionUtils.isEmpty(freshmanExcelList)
                    || !CollectionUtils.isEmpty(testResultExcelArrayList)) {
                ExcelWriter excelWriter = EasyExcel.write(bos).excelType(ExcelTypeEnum.XLSX).build();
                WriteSheet writeSheet = EasyExcel.writerSheet(0, "批次表").head(BatchExcel.class).build();
                excelWriter.write(batchExcelList, writeSheet);
                WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "人员表").head(FreshmanExcel.class).build();
                excelWriter.write(freshmanExcelList, writeSheet1);
                WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "记录表").head(RecordExcel.class).build();
                excelWriter.write(testResultExcelArrayList, writeSheet2);

                excelWriter.finish();
                ZipEntry zipEntry = new ZipEntry("新人日志ETL数据表格.xlsx");
                zipOutputStream.putNextEntry(zipEntry);
                zipOutputStream.write(bos.toByteArray());
                bos.reset();

            }
            zipOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭数据流,注意关闭的顺序
            zipOutputStream.close();
            bos.close();
        }
    }

前端vue的话


      handleExportZip() {
        this.$confirm("是否导出压缩包?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          // 发送下载请求
          return exportZipFile(this.query); // 假设有一个与后端通信的导出压缩包的方法
        }).then((response) => {
          // 处理下载的响应
          // 解析文件名
          let fileName = "xrrj.zip";
          let contentDisposition = response.headers['content-disposition'] + "";
          let index = contentDisposition.indexOf('=');
          if (index > 0) {
            fileName = decodeURIComponent(contentDisposition.substr(index + 1));
          }

          // 创建一个 Blob 对象并下载文件
          const blob = new Blob([response.data], { type: 'application/zip' });
          const link = document.createElement('a');
          link.href = window.URL.createObjectURL(blob);
          link.download = fileName;
          link.click();
          URL.revokeObjectURL(blob);
        }).catch((error) => {
          // 处理下载失败的情况
          console.error("导出压缩包失败:", error);
          this.$message.error("导出压缩包失败");
        });
      }


                 导出ETL数据表格
                 

你可能感兴趣的:(笔记)