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

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

相关推荐
奋进的芋圆1 分钟前
DataSyncManager 详解与 Spring Boot 迁移指南
java·spring boot·后端
计算机程序设计小李同学15 分钟前
个人数据管理系统
java·vue.js·spring boot·后端·web安全
小刘爱搬砖1 小时前
SpringBoot3 + GraalVM安装和初次打包
spring boot·graalvm
_UMR_2 小时前
springboot集成Jasypt实现配置文件启动时自动解密-ENC
java·spring boot·后端
蓝色王者3 小时前
springboot 2.6.13 整合flowable6.8.1
java·spring boot·后端
hashiqimiya4 小时前
springboot事务触发滚动与不滚蛋
java·spring boot·后端
因我你好久不见4 小时前
Windows部署springboot jar支持开机自启动
windows·spring boot·jar
无关86885 小时前
SpringBootApplication注解大解密
spring boot
追梦者1237 小时前
springboot整合minio
java·spring boot·后端
帅气的你7 小时前
Spring Boot 集成 AOP 实现日志记录与接口权限校验
java·spring boot