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

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

相关推荐
像少年啦飞驰点、5 小时前
零基础入门 Spring Boot:从“Hello World”到可上线的 Web 应用全闭环指南
java·spring boot·web开发·编程入门·后端开发
有来技术5 小时前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端
东东5165 小时前
xxx医患档案管理系统
java·spring boot·vue·毕业设计·智慧城市
东东5166 小时前
学院个人信息管理系统 (springboot+vue)
vue.js·spring boot·后端·个人开发·毕设
qq_12498707537 小时前
基于Srpingboot心晴疗愈社平台的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·spring·microsoft·毕业设计·计算机毕业设计
大爱编程♡7 小时前
SpringBoot统一功能处理
java·spring boot·后端
小马爱打代码10 小时前
Spring Boot:第三方 API 调用的企业级容错设计
java·spring boot·后端
东东51611 小时前
xxx食堂移动预约点餐系统 (springboot+微信小程序)
spring boot·微信小程序·小程序·毕业设计·个人开发·毕设
csdn2015_12 小时前
springboot task
java·spring boot·后端
czlczl2002092512 小时前
Spring Boot :如何高性能地在 Filter 中获取响应体(Response Body)
java·spring boot·后端