1、配置pom文件

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

2、配置模板

image.png

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

image.png

 

GitHub 加速计划 / ea / easyexcel
27
5
下载
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:4 个月前 )
c42183df Bugfix 1 年前
efa7dff6 * 重新加回 `commons-io` 1 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐