基于EasyExcel、FastExcel封装spring boot starter

项目基于 https://github.com/pig-mesh/excel-spring-boot-starter 调整,可以先查看 官方文档 获取基础使用方法,本文只介绍新增的功能。

此项目底层基于 FastExcel 实现 Excel 的读写。

此项目1.2.10 & 3.1.3 版本及之前版本,底层基于 EasyExcel 实现 Excel 的读写。

版本更新日志可查看:CHANGELOG.md

0 maven仓库

项目已上传至 maven 仓库,直接引入即可使用,最新版本可以查询中央仓库

版本 支持
3.x.x 适配 SpringBoot3.x
1.x.x 适配 SpringBoot2.x
xml 复制代码
<dependency>
  <groupId>com.kangaroohy</groupId>
  <artifactId>excel-spring-boot-starter</artifactId>
  <version>${lastVersion}</version>
</dependency>

1 配置增强

1.1 密码

支持全局yaml配置(kangaroohy.excel.password)

1.2 水印

支持全局yaml配置(kangaroohy.excel.water-mark),也支持单个方法配置

java 复制代码
@RequestMapping("/water-mark")
@RestController
public class ExcelWaterMarkTestController {
    
    @ResponseExcel(name = "用户记录", sheets = {
        @Sheet(sheetName = "用户信息")
    }, waterMark = "这是水印")
    @GetMapping
    public List<User> exportWithWaterMark() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            userList.add(User.builder()
                    .id((long) i)
                    .username("username" + i)
                    .password("password" + i)
                    .build());
        }
        return userList;
    }
}

2 注解增强

2.1 添加批注

在需要添加批注的属性上配置即可

java 复制代码
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelNotation {
  /**
   * 文本内容
   */
  String value() default "";
}

2.2 标识是否必填

此处只是简单的改变表头字体颜色,醒目提醒用户字段必填

java 复制代码
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelRequired {
  /**
   * 字体颜色
   */
  IndexedColors frontColor() default IndexedColors.RED;
}

2.3 设置单元格下拉框

可用于提醒用户,此单元格的可选值

java 复制代码
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelSelector {
  /**
   * 固定数据
   */
  String[] value() default {};
  /**
   * 字典key
   */
  String dictKeyValue() default "";
  /**
   * 服务类,需要交给spring管理,如 @Service,如果只有一个实现类时,此属性可以不设置
   */
  Class<? extends ExcelSelectorService> serviceClass() default ExcelSelectorService.class;
  /**
   * 设置下拉框的起始行,默认为表头的下一行
   */
  int firstRow() default -1;
  /**
   * 设置下拉框的结束行,默认为int最大值,65535
   */
  int lastRow() default 0x10000;
}

2.4 导入时校验数据是否重复

数据导入解析时校验数据使用

java 复制代码
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelUniqueFields {
  /**
   * 该组内所有字段组合保证唯一性
   *
   * @return
   */
  String[] groupUnique() default {};
  /**
   * 该组内每个字段单独保证唯一,字段之间可以允许重复
   *
   * @return
   */
  String[] singleUnique() default {};
  /**
   * 空值不做校验,false时 null 也会当成值的一种参与校验
   *
   * @return
   */
  boolean skipNull() default true;
}

以上所有更新的一个集中演示

相关推荐
海兰35 分钟前
【第21篇-续】graph-Stream-Node改造为适配openAI模型示例
java·人工智能·spring boot·spring·spring ai
Albert Edison1 小时前
基于 SpringBoot + RabbitMQ 完成企业级应用通信
spring boot·rabbitmq·java-rabbitmq
happymaker06263 小时前
Spring学习日记——DAY03(yml文件)
java·spring boot·spring
hikktn4 小时前
企业级Spring Boot应用管理:从零打造生产级启动脚本
java·spring boot·后端
霸道流氓气质4 小时前
Spring Boot + MyBatis-Plus 实现异常隔离的 Upsert 数据落库(含远程调用数据补全)
spring boot·后端·mybatis
不懂的浪漫4 小时前
01|从 Spring Boot 项目理解 RAG:ingest、query、rerank、trace 到 eval
java·人工智能·spring boot·后端·ai·rag
__log4 小时前
NestJS vs Spring Boot:从架构哲学到实战选择的技术全景解析
spring boot·后端·架构·typescript
拽着尾巴的鱼儿5 小时前
国密算法 Spring Boot 实战:SM2/SM3/SM4 完整集成指南
spring boot·后端·算法
Devin~Y5 小时前
大厂Java面试实战:Spring Boot微服务、Redis缓存、Kafka消息队列与Spring AI RAG
java·spring boot·redis·kafka·mybatis·spring mvc·hikaricp
weixin_489690025 小时前
NAS部署实测:Solon vs Spring Boot,从内存到包体积的“降维打击”
java·spring boot·后端