基于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;
}

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

相关推荐
Q_Q5110082857 小时前
python+uniapp基于微信小程序团购系统
spring boot·python·微信小程序·django·uni-app·node.js·php
重生之我在二本学院拿offer当牌打8 小时前
手写SpringBoot Starter(一):10分钟带你入门,从此告别重复配置!
spring boot
初见0018 小时前
🌱 SpringBoot自动配置:别装了,我知道你的秘密!🤫
spring boot·后端
lang201509288 小时前
Spring Boot核心功能深度解析
spring boot
计算机毕业设计小帅9 小时前
【2026计算机毕业设计】基于Springboot的校园失物招领小程序
spring boot·小程序·课程设计
7哥♡ۣۖᝰꫛꫀꪝۣℋ11 小时前
Spring Boot
java·spring boot·后端
lang2015092811 小时前
Spring Boot开发最佳实践速览
spring boot
哲此一生98411 小时前
创建一个SpringBoot项目(连接数据库)
java·spring boot·后端
摇滚侠11 小时前
Spring Boot 3零基础教程,WEB 开发 HttpMessageConverter @ResponseBody 注解实现内容协商源码分析 笔记33
java·spring boot·笔记
计算机毕业设计小帅11 小时前
【2026计算机毕业设计】基于Springboot的校园电动车短租平台
spring boot·后端·课程设计