easyexcel按照模板导出数据

1、配置pom文件

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.9</version>
</dependency>

2、配置模板

3、编码


4、效果 

response.reset();  
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            ExcelWriter build = EasyExcel.write(out).withTemplate(classPathResource.getFile()).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
//配置使用上一个行的样式
            WriteSheet writeSheet1 = EasyExcel.writerSheet().registerWriteHandler(new MyHandler()).build();
            FillConfig fillConfig1 = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
//有集合需要写,则forceNewRow(true)
            FillConfig fillConfig2 = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            Map map = JSONObject.parseObject(JSONObject.toJSONString(byId), Map.class);
            build.fill(map, fillConfig1, writeSheet1);
            for (int i = 0; i < byId.getSugarCookingProcessEntities().size(); i++) {
                byId.getSugarCookingProcessEntities().get(i).setId(i + 1L);
            }
            List list = JSONObject.parseObject(JSONObject.toJSONString(byId.getSugarCookingProcessEntities()), List.class);
            build.fill(new FillWrapper("sugarCookingProcessEntities", list), fillConfig2, writeSheet1);
            build.fill(new FillWrapper("sugarCookingFeedingEntities", JSONObject.parseObject(JSONObject.toJSONString(byId.getSugarCookingFeedingEntities()), List.class)), fillConfig2, writeSheet1);
            IOUtils.write(out.toByteArray(), response.getOutputStream());
        } catch (Exception e) {
            log.error("导出数据" + byId.getOrderId() + "失败", e);
        }
经验分享 程序员 微信小程序 职场和发展