1.添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
<exclusion>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
2.添加excel模板
注意框出来的两个地方的不同点 {} 与 {.} 为两种不同的填充方式,由于我的项目是springboot,所以这个模板我就放在resource/static下,看你项目情况放吧
3.前面说的两种填充方式:{}里面对应的是map的键值对,{}里面与键名匹配就自动填入、{.}对应的是List<T>的对象属性,{.}里面要与对象的属性字段名对应
具体分三步走,
1.构建好所需要map与list
2.获取excel模板
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/liusuDayReportTemplate.xlsx");
ServletOutputStream out = response.getOutputStream();
ExcelWriter excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
3.填充list与map并导出
//列表填充
excelWriter.fill(dayReportListAndAll.getList(), writeSheet);
//值填充
Map<String, Object> map = new HashMap<>();
map.put("reportDate", DateFormatConstant.yyyyMMdd.format(reportDate));
map.put("stayDriverNeidiNumAll", dayReportListAndAll.getStayDriverNeidiNumAll());
map.put("stayDriverNeidiFocusNumAll", dayReportListAndAll.getStayDriverNeidiFocusNumAll());
map.put("stayDriverNeidiAppointNumAll", dayReportListAndAll.getStayDriverNeidiAppointNumAll());
map.put("wgDriverNeidiNumAll", dayReportListAndAll.getWgDriverNeidiNumAll());
map.put("stayDriverXianggangNumAll", dayReportListAndAll.getStayDriverXianggangNumAll());
map.put("stayDriverXianggangFocusNumAll", dayReportListAndAll.getStayDriverXianggangFocusNumAll());
map.put("stayDriverXianggangAppointNumAll", dayReportListAndAll.getStayDriverXianggangAppointNumAll());
map.put("wgDriverXianggangNumAll", dayReportListAndAll.getWgDriverXianggangNumAll());
map.put("stayDriverNumAll", dayReportListAndAll.getStayDriverNumAll());
map.put("wgDriverNumAll", dayReportListAndAll.getWgDriverNumAll());
excelWriter.fill(map, writeSheet);
//开始导出
excelWriter.finish();
完成