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

}
相关推荐
赵_叶紫10 分钟前
Node.js 知识点梳理与实战代码
前端
IT_陈寒21 分钟前
JavaScript这5个隐藏技巧,90%的开发者都不知道!
前端·人工智能·后端
明月_清风1 小时前
小程序云函数:从入门到全栈的“降维打击”指南
前端·微信小程序·小程序·云开发
wuhen_n2 小时前
告别 Options API:为什么 Composition API 是逻辑复用的未来?
前端·javascript·vue.js
明月_清风2 小时前
前端异常捕获:从“页面崩了”到“精准定位”的实战架构
前端·javascript·监控
wuhen_n2 小时前
高效的数据解构:用 toRefs 和 toRef 保持响应性
前端·javascript·vue.js
小兵张健12 小时前
价值1000的 AI 工作流:Codex 通用前端协作模式
前端·aigc·ai编程
sunny_12 小时前
面试踩大坑!同一段 Node.js 代码,CJS 和 ESM 的执行顺序居然是反的?!99% 的人都答错了
前端·面试·node.js
拉不动的猪12 小时前
移动端调试工具VConsole初始化时的加载阻塞问题
前端·javascript·微信小程序
NE_STOP13 小时前
MyBatis-配置文件解读及MyBatis为何不用编写Mapper接口的实现类
java