使用hutools 生成excel

一.引入依赖

复制代码
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.3</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

二.代码

java 复制代码
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class User {

    
    @ApiModelProperty(value = "用户ID")
    private String id;
    
    @ApiModelProperty(value = "用户名称")
    private String name;
    
    @ApiModelProperty(value = "用户年龄")
    private Integer age;
    
    @ApiModelProperty(value = "用户邮箱")
    private String email;
}
java 复制代码
 private void writeExcel(List<CarVdnEntity> excelList) {
        log.info("CarVdnServiceImpl.writeExcel.excelList.size:", excelList.size());
        LinkedList<String> headerList = new LinkedList<>();
        //生成文件路径 windows 环境可以写成("D://file//")
        ExcelWriter writer = ExcelUtil.getWriter("GBOM-" + (LocalDate.now().toString()));
        addHeaderAlias(writer, headerList);
        writer.writeHeadRow(headerList);
        //一次性写出内容,强制输出标题
        writer.write(excelList);
        //关闭writer,释放内存
        writer.close();
    }

    private static String convertToUnderscore(String camelCase) {
        // 使用正则表达式将大写字母前面添加下划线,并转换为小写
        String underscore = camelCase.replaceAll("([A-Z])", "_$1").toLowerCase();
        // 如果开头有下划线,则去除开头的下划线
        if (underscore.startsWith("_")) {
            underscore = underscore.substring(1);
        }
        return underscore;
    }

    private static void addHeaderAlias(ExcelWriter writer, LinkedList<String> header) {
        Class<?> clazz = User.class;
        Field[] fields = clazz.getDeclaredFields();
        // 遍历字段数组,获取每个字段的名称并添加到列表中
        for (Field field : fields) {
            String fieldName = field.getName();
            header.add(convertToUnderscore(fieldName));
            writer.addHeaderAlias(fieldName, convertToUnderscore(fieldName));
        }
    }

测试类:

java 复制代码
        LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
        // 结果集合
        List<User> excelList = new ArrayList<>();
        excelList.add(new User("1","张三",20,"zhangsan@163.com"));
        //写入excel
        writeExcel(excelList);
相关推荐
ratel️1 天前
小墨析 · Office Agent 日常办公不烧心
python·langchain·word·excel·fastapi·js·ppt
Logic1011 天前
没有TextSplit函数的老版本也能拆分单元格的内容
excel·拆分数据·excel技巧·数据拆分·拆分单元格·textsplit·旧版excel
2601_967987371 天前
你们都用Python实现了哪些自动化办公?
python·excel·自动化办公·1024程序员节·学习资料·跨平台操作
城数派1 天前
1901-2025年中国省/市/县三级逐月平均气温数据Excel+Shp格式
excel
gtstore2 天前
从 Excel 迁移到仓库管理系统的数据准备与切换方案
excel·数据迁移·仓库管理系统·进销存·信息化实施
ms365copilot3 天前
我不再自己做 Word、Excel 和 PPT 了,我让 Copilot 直接帮我创建 (1)
word·powerpoint·excel·copilot
cspttty3 天前
2027采购岗求职:大数据管理与应用专业如何把Excel、SQL变成业务能力
excel
ms365copilot4 天前
Excel Copilot 一键定位修改的内容
excel·copilot
帆张芳显4 天前
前端表格公式能力新选择|智表 ZCell 公式引擎深度剖析
前端·excel·canva可画·excel插件
地图小课堂4 天前
Bigemap Pro 导入 Excel、CSV、TXT 点位表格教程
excel