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

}
相关推荐
SsunmdayKT8 分钟前
前后端项目部署与运行机制全流程详解
前端·后端
本末倒置1838 分钟前
Vue 3 开发者转型 React 指南:保姆级教程
前端·javascript·vue.js
Reart11 分钟前
从0解构tinyWeb项目--(Day:10)
前端·后端·架构
代码漫谈32 分钟前
一文学习 SpringBoot 的 application.yml 配置,基于 Spring Boot 3.2.x
java·spring boot·spring·配置文件
SamDeepThinking33 分钟前
程序员如何接受工作内容毫无意义?
java·后端·程序员
三翼鸟数字化技术团队1 小时前
基于Redis ZSet实现分布式优先级队列的技术实践
java·redis
无所事事O_o1 小时前
加密过程及原理浅析
java·加密
牛蛙点点申请出战1 小时前
IconFontViewer -- 一个可以在 Android Studio 中实时预览 IconFont 的插件
android·前端·intellij idea
2301_771717211 小时前
最近在刷牛客:使用Spring AOP实现性能监控时
java·后端·spring