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

}
相关推荐
sunxunyong37 分钟前
CGroup配置
linux·运维·服务器
靓仔建1 小时前
Vue3导入组件出错does not provide an export named ‘user_setting‘ (at index.vue:180:10)
开发语言·前端·typescript
HalvmånEver1 小时前
7.高并发内存池大页内存申请释放以及使用定长内存池脱离new
java·spring boot·spring
EnoYao1 小时前
我写了一个团队体检报告 Skill,把摸鱼的同事扒出来了😅
前端·javascript
凤山老林1 小时前
SpringBoot 使用 H2 文本数据库构建轻量级应用
java·数据库·spring boot·后端
梁正雄1 小时前
Python前端-2-css练习
前端·css·python
清汤饺子1 小时前
用 Cursor 半年了,效率还是没提升?是因为你没用对这 7 个功能
前端·后端·cursor
蓝莓味的口香糖2 小时前
【vue3】组件的批量全局注册
前端·javascript·vue.js
wefly20172 小时前
开发者效率神器!jsontop.cn一站式工具集,覆盖开发全流程高频需求
前端·后端·python·django·flask·前端开发工具·后端开发工具
赶路人儿2 小时前
UTC时间和时间戳介绍
java·开发语言