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

}
相关推荐
回忆是昨天里的海2 小时前
docker常见命令
java·docker·容器
计算机毕设vx_bysj68693 小时前
计算机毕业设计必看必学~Springboot教学进度管理系统,原创定制程序、单片机、java、PHP、Python、小程序、文案全套、毕设成品等!
java·spring boot·vue·课程设计·管理系统
鱼锦0.03 小时前
在vue2中主页面怎么给子页面传递数据
前端·javascript·html
狂团商城小师妹3 小时前
JAVA外卖霸王餐CPS优惠CPS平台自主发布小程序+公众号霸王餐源码
java·开发语言·小程序
q***11654 小时前
Spring 中的 @ExceptionHandler 注解详解与应用
java·后端·spring
心软小念5 小时前
用Python requests库玩转接口自动化测试!测试工程师的实战秘籍
java·开发语言·python
u***j3246 小时前
后端服务限流实现,Redis+Lua脚本
java·redis·lua
CoderYanger6 小时前
A.每日一题——2536. 子矩阵元素加 1
java·线性代数·算法·leetcode·矩阵
不可描述的两脚兽6 小时前
Redis 快记
java·数据库·redis