SpringBoot的excel模板导出

Word的模板导出(参考:https://easyexcel.opensource.alibaba.com/docs/current/quickstart/fill)

  1. 创建有两个sheet的excel文件模板
    SpringBoot的excel模板导出_第1张图片
    SpringBoot的excel模板导出_第2张图片
  2. 将模板文件放入resource\templates/doc下
  3. 使用
    public void exportUavInfoExcel(HttpServletResponse response, CaseExportRPO caseExportRPO) throws IOException {

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("无人机情况汇报.xls", "utf-8"));

        // 获取数据库数据
        List<Object> caseList = baseMapper.getList();
        List<UavAreaExcel> typeList = exportStatisticsHandle(typeMap);

		// 获取模板
        String templateFileName = new ClassPathResource("templates/doc/uavInfoExcel.xls").getFile().getAbsolutePath();

        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFileName).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet("按区域").build();
            excelWriter.fill(areaList, writeSheet);
            WriteSheet writeSheet1 = EasyExcel.writerSheet("按类型").build();
            excelWriter.fill(typeList, writeSheet1);

            // 写入时间
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("timeRange", DateUtil.date(caseExportRPO.getStartTime()).toString(DatePattern.CHINESE_DATE_PATTERN) + " 至 " + DateUtil.date(caseExportRPO.getEndTime()).toString(DatePattern.CHINESE_DATE_PATTERN));
            excelWriter.fill(map, writeSheet);
            excelWriter.fill(map, writeSheet1);

            excelWriter.finish();
        }



    }

你可能感兴趣的:(其他,java,spring,boot,后端,java)