使用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);
相关推荐
杜大哥6 小时前
如何在WPS打开的word、excel文件中,使用AI?
人工智能·word·excel·wps
@LitterFisher8 小时前
Excell 代码处理
前端·javascript·excel
winfredzhang9 小时前
Python实战:Excel中文转拼音工具开发教程
python·安全·excel·汉字·pinyin·缩写
oh,huoyuyan10 小时前
火语言RPA--Excel插入空列
excel·rpa
企鹅侠客1 天前
开源免费文档翻译工具 可支持pdf、word、excel、ppt
人工智能·pdf·word·excel·自动翻译
小白今天也很酷1 天前
Python与MCU通信:串口数据采集及CSV/Excel存储方法
python·单片机·excel
xlxxy_1 天前
ABAP数据库表的增改查
开发语言·前端·数据库·sql·oracle·excel
程序猿多布1 天前
Unity Excel导表工具转Lua文件
unity·excel
The god of big data2 天前
Excel核心函数VLOOKUP全解析:从入门到精通
excel
专注VB编程开发20年2 天前
除了 EasyXLS,加载和显示.xlsx 格式的excel表格,并支持单元格背景色、边框线颜色和粗细等格式化特性
c++·windows·excel·mfc·xlsx