本文介绍: 【代码】EasyExcel导出多个sheet 并完成对指定sheet页进行操作。

直接上效果图:

实体类代码:

SheetInfoBean,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;
}

主要代码逻辑:


            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加批注操作

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

原文地址:https://blog.csdn.net/XikYu/article/details/135826852

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_61781.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注