基于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 小时前
【从SpringBoot到SpringCloud】
java·spring boot·spring cloud
皮卡丘不断更1 小时前
手搓本地 RAG:我用 Python 和 Spring Boot 给 AI 装上了“实时代码监控”
人工智能·spring boot·python·ai编程
lucky67072 小时前
Spring Boot集成Kafka:最佳实践与详细指南
spring boot·kafka·linq
Coder_Boy_2 小时前
基于Spring AI的分布式在线考试系统-事件处理架构实现方案
人工智能·spring boot·分布式·spring
毕设源码-钟学长4 小时前
【开题答辩全过程】以 基于Springboot的扶贫众筹平台为例,包含答辩的问题和答案
java·spring boot·后端
Java水解5 小时前
Spring Boot 4 升级指南:告别RestTemplate,拥抱现代HTTP客户端
spring boot·后端
神云瑟瑟5 小时前
spring boot拦截器获取requestBody的最佳实践
spring boot·拦截器·requestbody
暮色妖娆丶5 小时前
Spring 源码分析 BeanFactoryPostProcessor
spring boot·spring·源码
南极企鹅6 小时前
springBoot项目有几个端口
java·spring boot·后端
忧郁的Mr.Li6 小时前
SpringBoot中实现多数据源配置
java·spring boot·后端