当前位置: 首页 > news >正文

Easy Excel

Easy Excel

    • 一、依赖引入
    • 二、基本使用
      • 1. 定义实体类(导入/导出共用)
      • 2. 写 Excel
      • 3. 读 Excel
    • 三、常用注解说明(完整列表)
    • 四、进阶:自定义转换器(Converter)
  • 其它
    • 自定义转换器没生效

Easy Excel在POI的基础上进行了优化。
EasyExcel 是阿里巴巴开源的 Java Excel 读写工具,相比 Apache POI 更加轻量和高效。它广泛用于大数据量的 Excel 导入导出场景,特别适合 Spring Boot 项目中使用。

下面是 EasyExcel 的常用使用方式和注解说明:


一、依赖引入

<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.3.2</version>
</dependency>

二、基本使用

1. 定义实体类(导入/导出共用)

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;import java.util.Date;@Data
@ColumnWidth(20) // 设置列宽
public class UserExcel {@ExcelProperty("ID")private Long id;@ExcelProperty("用户名")private String username;@ExcelProperty("创建时间")@DateTimeFormat("yyyy-MM-dd HH:mm:ss")private Date createTime;
}

2. 写 Excel

List<UserExcel> dataList = ...;String fileName = "用户信息.xlsx";
EasyExcel.write(fileName, UserExcel.class).sheet("用户列表").doWrite(dataList);

3. 读 Excel

public class UserExcelListener extends AnalysisEventListener<UserExcel> {@Overridepublic void invoke(UserExcel data, AnalysisContext context) {System.out.println("读取到一条数据:" + data);// 可写入数据库或缓存等}@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {System.out.println("所有数据读取完毕");}
}
String fileName = "用户信息.xlsx";
EasyExcel.read(fileName, UserExcel.class, new UserExcelListener()).sheet().doRead();

三、常用注解说明(完整列表)

注解作用
@ExcelProperty(value = "列名", index = 0)设置列名和顺序索引
@DateTimeFormat("yyyy-MM-dd")格式化日期导出
@NumberFormat("#.##")数字格式化导出
@ExcelIgnore忽略该字段
@ColumnWidth(20)设置导出列宽
@ContentStyle(...)设置内容样式(字体、对齐、颜色)
@HeadStyle(...)设置表头样式
@ExcelIgnoreUnannotated类上标注,未标注 @ExcelProperty 的字段不导出
@ExcelImage导出图片
@ExcelEnum(需额外扩展)枚举值转中文等映射(通常结合 Converter 使用)

四、进阶:自定义转换器(Converter)

如:将性别枚举类型转换为字符串
GenderConverter是一个自定义的转换器,结合@ExcelProperty(value = "性别", converter = GenderConverter.class)converter属性,跳转到GenderConverter处理值

public class GenderConverter implements Converter<GenderEnum> {@Overridepublic Class<GenderEnum> supportJavaTypeKey() {return GenderEnum.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}@Overridepublic CellData<String> convertToExcelData(GenderEnum value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {return new CellData<>(value.getDesc());}@Overridepublic GenderEnum convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {return GenderEnum.fromDesc(cellData.getStringValue());}
}

然后在实体类中使用:

@ExcelProperty(value = "性别", converter = GenderConverter.class)
private GenderEnum gender;

其它

自定义转换器没生效

要用converter指定
@ExcelProperty(value = “性别”, converter = GenderConverter.class)

相关文章:

  • c语言(持续更新)
  • 使用DataX同步MySQL数据
  • OSPF域内路由
  • matlab时序预测并绘制预测值和真实值对比曲线
  • 6.9本日总结
  • DPC密度峰值聚类
  • PostgreSQL 与 SQL 基础:为 Fast API 打下数据基础
  • 卷积神经网络设计指南:从理论到实践的经验总结
  • CppCon 2015 学习:STL Algorithms in Action
  • CppCon 2015 学习:Transducers, from Clojure to C++
  • 时间同步技术在电力系统中的应用
  • 【习题】DevEco Studio的使用
  • 驭码 CodeRider 2.0 产品体验:智能研发的革新之旅
  • 用递归算法解锁「子集」问题 —— LeetCode 78题解析
  • 蓝桥杯 冶炼金属
  • 基于matlab策略迭代和值迭代法的动态规划
  • ubuntu清理垃圾
  • MySQL-事务(TRANSACTION-ACID)管理
  • Java 与 MySQL 性能优化:MySQL 慢 SQL 诊断与分析方法详解
  • 数字ic后端设计从入门到精通6(含fusion compiler, tcl教学)repeater详解
  • 关于做网站建设公司你应该知道的/怎样建网站平台
  • 网站建设方案多少钱/app软件开发
  • 媒体广告投放平台/windows优化大师卸载不了
  • 猎聘网网站谁做的/seo网站推广助理
  • 房屋自建设计哪个网站好/快速排名推荐
  • 网站服务器租用还是自买/新闻软文广告