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;
    }
相关推荐
麻芝汤圆36 分钟前
利用Hadoop MapReduce实现流量统计分析
大数据·开发语言·hadoop·分布式·servlet·mapreduce
局外人_Jia40 分钟前
【 C# 使用 MiniExcel 库的典型场景】
开发语言·windows·c#·miniexcel
virelin_Y.lin1 小时前
系统与网络安全------Windows系统安全(10)
windows·web安全·系统安全·活动目录·
风象南1 小时前
SpringBoot实现接口防刷的5种实现方案
java·spring boot·后端
virelin_Y.lin1 小时前
系统与网络安全------Windows系统安全(8)
windows·web安全·系统安全·dns
云之兕1 小时前
Spring Boot 自动配置原理详解
java·前端·spring boot
烁3471 小时前
每日一题(小白)暴力娱乐篇20
java·开发语言·算法·排序算法·娱乐
heyCHEEMS1 小时前
01背包 Java
java·算法·深度优先
呦呦鹿鸣Rzh1 小时前
SpringMvc的请求-获得请求参数
java·开发语言
头孢头孢1 小时前
go语言的语法糖以及和Java的区别
java·开发语言·golang