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;
    }
相关推荐
DCTANT19 分钟前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.29 分钟前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超38 分钟前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice1 小时前
对象的finalization机制Test
java·开发语言·jvm
专注VB编程开发20年2 小时前
开机自动后台运行,在Windows服务中托管ASP.NET Core
windows·后端·asp.net
望获linux2 小时前
【实时Linux实战系列】CPU 隔离与屏蔽技术
java·linux·运维·服务器·操作系统·开源软件·嵌入式软件
JosieBook2 小时前
【Java编程动手学】使用IDEA创建第一个HelloJava程序
java·开发语言·intellij-idea
Thomas_YXQ2 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
summer夏1232 小时前
2025.07 做什么
java·android studio
钢铁男儿3 小时前
C# 委托(调用带引用参数的委托)
java·mysql·c#