web导出压缩包(多文件压缩到压缩包后导出)

public void exportZip(String time, Integer courType, HttpServletResponse response) {
        ZipOutputStream zipOutputStream = null;
        try {
            Date dateTime = DateUtil.beginOfMonth(DateUtil.parse(time));
            DateTime beginTime = DateUtil.beginOfMonth(dateTime);
            DateTime endTime = DateUtil.endOfMonth(dateTime);
            response.setContentType("application/force-download;charset=UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("月度汇总", "utf-8") + ".zip");
            zipOutputStream = new ZipOutputStream(response.getOutputStream());
            exportAnalysisReportZip(beginTime, endTime, courType, zipOutputStream);
            exportTraceability(beginTime, endTime, courType, zipOutputStream);
            zipOutputStream.closeEntry();
        } catch (Exception e) {
            log.error("", e);
        } finally {
            try {
                if (zipOutputStream != null) {
                    zipOutputStream.close();
                }
            } catch (IOException e) {
                log.error("", e);
            }
        }
    }

public void exportAnalysisReportZip(DateTime beginTime, DateTime endTime, Integer courType, ZipOutputStream zipOutputStream) throws IOException {
        AnalysisReportDto analysisReportDto = getAnalysisReportDto(beginTime, endTime, courType);
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("template/demo.docx");
        Configure config = Configure.builder().bind("orgcourselist", new LoopRowTableRenderPolicy())
                .bind("teclorgathiratiolist", new LoopRowTableRenderPolicy())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(is, config).render(analysisReportDto);

        zipOutputStream.putNextEntry(new ZipEntry("常态化.docx"));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        template.write(bos);
        byte[] brray = bos.toByteArray();
        InputStream in = new ByteArrayInputStream(brray);
        IoUtil.copy(in, zipOutputStream);
        IoUtil.close(in);
    }

    public void exportTraceability(DateTime courBeginTime, DateTime courEndTime, Integer courType, ZipOutputStream zipOutputStream) {
        InputStream in = null;
        try {
            AttendanceHistorySearchDo attendanceHistorySearchDo = new AttendanceHistorySearchDo();
            List<AttendanceHistory> list = attendanceHistoryDao.findListByPo(attendanceHistorySearchDo, "cour_begin_time", "ASC", null, null, false);
            List<AttendanceHistoryExcelDto> attendanceHistoryExcelDtos = new ArrayList<>();
            list.stream().forEach(dto -> {
                AttendanceHistoryExcelDto attendanceHistoryExcelDto = new AttendanceHistoryExcelDto();
                BeanUtils.copyProperties(dto, attendanceHistoryExcelDto);
                attendanceHistoryExcelDtos.add(attendanceHistoryExcelDto);
            });
            zipOutputStream.putNextEntry(new ZipEntry("溯源数据.xlsx"));
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            EasyExcel.write(bos, AttendanceHistoryExcelDto.class).sheet("行数据").doWrite(attendanceHistoryExcelDtos);
            byte[] brray = bos.toByteArray();
            in = new ByteArrayInputStream(brray);
            IoUtil.copy(in, zipOutputStream);
        } catch (IOException e) {
            log.error("", e);
        } finally {
            IoUtil.close(in);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值