EasyExcel使用模板导出复杂Excel

1)添加easyexlce的依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.0.0-beta1</version>
</dependency>

2)添加excel模板文件
在这里插入图片描述
实现的效果:
在这里插入图片描述

3)导出

@GetMapping("/export/use/template")
public void downloadUseTemplate(HttpServletResponse response) throws Exception {
   
    // 下载excel
    String fileName = "使用模板导出.xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

    // 加载模板
    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
    // 输出到response
    ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
            .withTemplate(inputStream)
            .excelType(ExcelTypeEnum.XLSX).build();

    // 得到sheet
    WriteSheet writeSheet = EasyExcel.writerSheet().build();

    // 单个字段填充
    Map<String, Object> map = new HashMap<>();
    map.put("weekAdd",100);
    map.put("monthAdd",200);
    map.put("total",300);
    excelWriter.fill(map, writeSheet);

    // 列表竖向填充
    List<DetailDTO> details = new ArrayList<>();
    details.add(new DetailDTO(new SimpleDateFormat("yyyy-MM-dd").parse("2023-12-10"), 100));
    details.add(new DetailDTO(new SimpleDateFormat("yyyy-MM-dd").parse("2023-12-11"), 80));
    details.add(new DetailDTO(new SimpleDateFormat("yyyy-MM-dd").parse("2023-12-12"), 200));
    excelWriter.fill(new FillWrapper("details", details), writeSheet);

    // 列表横向填充
    FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
    excelWriter.fill(new FillWrapper("week", details),fillConfig, writeSheet);

    excelWriter.finish();

}

完整的源码下载https://github.com/xjs1919/enumdemo下面的easyexcel-upload-demo
done!

相关推荐

  1. springboot使用EasyExcel实现Excel导入导出

    2023-12-12 09:16:02       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-12 09:16:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-12 09:16:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-12 09:16:02       82 阅读
  4. Python语言-面向对象

    2023-12-12 09:16:02       91 阅读

热门阅读

  1. 数据结构 | Floyd

    2023-12-12 09:16:02       62 阅读
  2. MySQL全文索引布尔模式详解

    2023-12-12 09:16:02       50 阅读
  3. 【Go自学版】03-即时通信系统3

    2023-12-12 09:16:02       56 阅读
  4. Hadoop 完全分布式搭建 详细流程

    2023-12-12 09:16:02       62 阅读
  5. Kubernetes实战(十一)-重装Kubernetes集群

    2023-12-12 09:16:02       50 阅读