多个文件导出

多表导出结合easyExcel

https://blog.csdn.net/L1569850979/article/details/110671240

导出工具类

   /**
     * 一键导出所有数据多个sheet
     * @author zhenhuaxiang
     */
    public void downloadAll(HttpServletResponse response, Map<String,List<?>> map, String fileNames) throws IOException {
        if (ObjectUtils.isEmpty(map)) {
            throw new RuntimeException();
        }
        if (StringUtils.isEmpty(fileNames)) {
            fileNames = new Date().toString();
        }
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode(fileNames, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            //
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
            int sheetNo = 0;
            for (Map.Entry<String,List<?>> entry:map.entrySet()
                 ) {
                String sheetName = entry.getKey();
                List<?> list = entry.getValue();
                excelWriter.write(list, EasyExcel.writerSheet(sheetNo,sheetName).head(list.get(0).getClass()).build());
                sheetNo++;
            }
            // 关闭流
            excelWriter.finish();
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> hashMap = new HashMap<String, String>(16);
            hashMap.put("status", "failure");
            hashMap.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }

使用示例

     /**
     * 一键导出所有数据多个sheet
     */
    public void downloadAll(HttpServletResponse response, String examId) throws IOException {
        if (StringUtils.isEmpty(examId)) {
            throw new BusinessException(2002, "请选择考试批次!!!");
        }
        Map<String, List<?>> map = new HashMap<>(16);

        List<SchoolOneTwoScore> listOne = getSchoolOneTwoScores(examId);

        List<CityBatchOnline> listTwo = getCityBatchOnlines(examId);

        List<CitySingleScore> listThree = getCitySingleScores(examId);

        List<CitySubjectScore> listFour = getCitySubjectScores(examId);

        List<TotalScoreSubsection> listFive = scoreDownService.getTotalScoreSubsections(examId, AdsSubsection.ONESTR);
        List<TotalScoreSubsection> listSix = scoreDownService.getTotalScoreSubsections(examId, AdsSubsection.TWOSTR);

        List<SchoolSubjectCompare> listSeven = scoreDownService.getSchoolSubjectCompares(examId);

        map.put("学校一段二段统计表", listOne);
        map.put("全市批次上线统计表", listTwo);
        map.put("全市单科成绩统计表", listThree);
        map.put("全市学科一分一段表", listFour);
        map.put("全市总分一分一段表(原始成绩)", listFive);
        map.put("全市总分一分一段表(赋分成绩)", listSix);
        map.put("学校学科对比表-等级分布", listSeven);
        easyExcelUtil.downloadAll(response, map, "全表导出");

    }

注意list中的实体使用了easyExcel注解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱上编程2705

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值