2020最简单的java导出excel,使用easyexcel和准备好的excel模板进行数据填充导出

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();

完成

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值