EasyExcel导出多个sheet 并完成对指定sheet页进行操作

直接上效果图:

实体类代码:

复制代码
SheetInfoBean,java
java 复制代码
package com.ly.cloud.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @Author 
 * @Date Created in  2024/1/19 12:27
 * @DESCRIPTION:  YxThingsExportDto - YzExportDto - ZxExportDto  三个sheet
 * @Version V1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SheetInfoBean {
    /**
     * sheet页名称
     */
    private String sheetName;

    /**
     * sheet标题bean
     */
    private Class<?> headClass;

    /**
     * sheet页数据
     */
    private List<?> dataList;
}

主要代码逻辑:

java 复制代码
            List<YxThingsExportDto> exportList = deCopyYxList(yxThingsList);
            List<ZxExportDto> zxExportList = deCopyZxList(zxList);
            List<YzExportDto> yzExportList = deCopyYzList(yzList);
            //上面这几个集合数3sheet表的数据  

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            // 构造各个sheet页相关信息
            List<SheetInfoBean> sheetList = new LinkedList<>();
            sheetList.add(new SheetInfoBean("事项信息", YxThingsExportDto.class, exportList));
            sheetList.add(new SheetInfoBean("材料", ZxExportDto.class, zxExportList));
            sheetList.add(new SheetInfoBean("类型", YzExportDto.class, yzExportList));

            long start = System.currentTimeMillis();
            // 自动列宽 + 批注
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
            CompletableFuture<Void> previousFuture = getVoidCompletableFuture(sheetList, excelWriter);

            previousFuture.join(); // 等待所有sheet写入完成
            excelWriter.finish(); // 关闭资源

            long end = System.currentTimeMillis();
            System.out.println(end - start);

写入 多个sheet页面 ;

写入 3个 sheet页 的数据;并给指定的sheet加批注操作

java 复制代码
 private static CompletableFuture<Void> getVoidCompletableFuture(List<SheetInfoBean> sheetList, ExcelWriter excelWriter) {
        ReentrantLock lock = new ReentrantLock(false);

        CompletableFuture<Void> previousFuture = CompletableFuture.completedFuture(null);

        for (SheetInfoBean bean : sheetList) {
            CompletableFuture<Void> currentFuture = new CompletableFuture<>();

            previousFuture.thenAcceptAsync(ignored -> {
                lock.lock();
                try {
                    WriteSheet writeSheet = EasyExcel.writerSheet(bean.getSheetName())
                            .head(bean.getHeadClass())
                            .build();
                    // 给第一个: 事项类别信息 sheet 页添加批注
                    if ("事项信息".equals(bean.getSheetName())) {
                        List<WriteHandler> list = new ArrayList<>();
                        list.add(new YxCommentWriteHandler());
                        writeSheet.setCustomWriteHandlerList(list);
                    }
                    excelWriter.write(bean.getDataList(), writeSheet);
                } catch (Exception e) {
                    throw new BusinessException(e);
                } finally {
                    lock.unlock();
                    currentFuture.complete(null);
                }
            });
            previousFuture = currentFuture;
        }
        return previousFuture;
    }
相关推荐
LUCIAZZZ41 分钟前
简单的SQL语句的快速复习
java·数据库·sql
komo莫莫da1 小时前
寒假刷题Day19
java·开发语言
S-X-S2 小时前
算法总结-数组/字符串
java·数据结构·算法
linwq82 小时前
设计模式学习(二)
java·学习·设计模式
桦说编程3 小时前
CompletableFuture 超时功能有大坑!使用不当直接生产事故!
java·性能优化·函数式编程·并发编程
@_@哆啦A梦3 小时前
Redis 基础命令
java·数据库·redis
字节全栈_rJF4 小时前
性能测试 —— Tomcat监控与调优:status页监控_tomcat 自带监控
java·tomcat
爱编程的小新☆5 小时前
Java篇之继承
java·开发语言
gentle coder5 小时前
Redis_Redission的入门案例、多主案例搭建、分布式锁进行加锁、解锁底层源码解析
java·redis·分布式
萝卜青今天也要开心5 小时前
读书笔记-《Redis设计与实现》(一)数据结构与对象(下)
java·数据结构·redis·学习