EasyExcel 导出Excel

maven

复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.3.3</version>
</dependency>

java

复制代码
package com.example.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.example.excel.model.User;
import com.example.excel.util.CustomCellWriteWidthHandle;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class EasyExcelExportExample {

    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        File file = new File("C:\\Workspace\\excel\\src\\main\\resources\\fz.png");

        URL url = file.toURI().toURL();
        List<User> dataList= new ArrayList<>();
        for(int i=1;i<=5000;i++){
            User u = new User();
            u.setId("id-"+i);
            u.setName("name"+i);
            u.setAge(i);
            u.setHeadImage(url);
            dataList.add(u);
        }


        ExcelWriter excelWriter = EasyExcel.write("example.xlsx").registerWriteHandler(new CustomCellWriteWidthHandle()).head(User.class).build();
        // 需要导出的数据总数
        int totalCount = dataList.size();
        // 每个sheet包含的数据量
        int sheetDataCount = 500;
        // 计算sheet数量
        int sheetCount = (totalCount + sheetDataCount - 1) / sheetDataCount;
        for (int i = 0; i < sheetCount; i++) {
            int dataStart = i * sheetDataCount;
            int dataEnd = Math.min(dataStart + sheetDataCount, totalCount);
            List<User> sheetData = dataList.subList(dataStart, dataEnd);
            WriteSheet writeSheet = EasyExcel.writerSheet(i, "数据"+(i+1)).build();
            excelWriter.write(sheetData, writeSheet);
        }
        excelWriter.finish();
    }

}
相关推荐
xflySnail12 分钟前
nas服务域名高速访问-DNS+ESA
运维·服务器·esa·无端口访问
海边的Kurisu1 小时前
苍穹外卖日记 | Day1 苍穹外卖概述、开发环境搭建、接口文档
java
king王一帅2 小时前
Incremark Solid 版本上线:Vue/React/Svelte/Solid 四大框架,统一体验
前端·javascript·人工智能
C雨后彩虹5 小时前
任务最优调度
java·数据结构·算法·华为·面试
heartbeat..5 小时前
Spring AOP 全面详解(通俗易懂 + 核心知识点 + 完整案例)
java·数据库·spring·aop
Jing_jing_X5 小时前
AI分析不同阶层思维 二:Spring 的事务在什么情况下会失效?
java·spring·架构·提升·薪资
元Y亨H7 小时前
Nacos - 服务发现
java·微服务
智航GIS7 小时前
10.4 Selenium:Web 自动化测试框架
前端·python·selenium·测试工具
微露清风7 小时前
系统性学习C++-第十八讲-封装红黑树实现myset与mymap
java·c++·学习
dasi02277 小时前
Java趣闻
java