【easy-excel-starter】一个注解自动导出对象为Excel 版本 1.2.3-JDK17

news/2024/7/21 5:40:41 标签: excel
excel-starter" rel="nofollow">介绍
版本 1.2.3-JDK17
wu-easy-excel-starter是一款面向对象的excel、文件导出框架。
实现的功能:
    1)针对Java中任何对象直接返回给web,在返回web过程自动将对象转换成想要的数据(Excel、File)
    2)前端传一个Excel过来,后端接口接收到的其实已经是Bean了

excel_10">快速导出excel模块

依赖引入
 <dependency>
    <groupId>com.wu</groupId>
    <artifactId>wu-easy-excel-starter</artifactId>
    <version>1.2.3-JDK17</version>
 </dependency>

基本用法

 @Data
 public class UseExcel {
 
     @EasyExcelFiled(name = "id")
     private Integer id;
 
     @EasyExcelFiled(name = "当前时间")
     private LocalDateTime currentTime;
 
     @EasyExcelFiled(name = "描述")
     private String desc;
 
     @EasyExcelFiled(name = "类型")
     private String type;
 }

    @EasyExcel(fileName = "导出数据")
    @GetMapping("/run/{size}")
    public List<UseExcel> run(@PathVariable Integer size) {
        List<UseExcel> useUserExcelList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            UseExcel useExcel = new UseExcel();
            useExcel.setCurrentTime(LocalDateTime.MAX);
            useExcel.setDesc("默认方式导出数据");
            useExcel.setId(i);
            useExcel.setType("默认方式双注解导出");
            useUserExcelList.add(useExcel);
        }
        return useUserExcelList;
    }

自定义字段注解用法

@Data
public class UseExcel {

    @JSONField(name = "id")
    private Integer id;

    @JSONField(name = "当前时间")
    private LocalDateTime currentTime;

    @JSONField(name = "描述")
    private String desc;

    @JSONField(name = "类型")
    private String type;
}
@EasyExcel(fileName = "非原生注解导出数据", filedColumnAnnotation = JSONField.class,filedColumnAnnotationAttribute = "name",multipleSheet = true, limit = 10,sheetShowContext = EasyExcel.SheetShowContext.TEXT)
@GetMapping("/run2/{size}")
public List<UseExcel> run2(@PathVariable Integer size) {
    List<UseExcel> useUserExcelList = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        UseExcel useExcel = new UseExcel();
        useExcel.setCurrentTime(LocalDateTime.MAX);
        useExcel.setDesc("自定义字段注解方式导出数据");
        useExcel.setId(i);
        useExcel.setType("自定义字段注解导出");
        useUserExcelList.add(useExcel);
    }
    return useUserExcelList;
}         
  • 说明 filedColumnAnnotation 从 JSONField注解的name属性中获取表头并导出数据
  • multipleSheet 导出的数据分多个sheet(工作簿)
  • limit每个sheet 有10条数据
  • sheetShowContext每个sheet的名称使用EasyExcel.SheetShowContext.TEXT 中文

自定义字段注解+自定控制器注解用法

@Data
public class UseExcel {

    @JSONField(name = "id")
    private Integer id;

    @JSONField(name = "当前时间")
    private LocalDateTime currentTime;

    @JSONField(name = "描述")
    private String desc;

    @JSONField(name = "类型")
    private String type;
}
  • 自定意义EasyExcel注解

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EasyExcel(useAnnotation = false,filedColumnAnnotation = JSONField.class)
public @interface EasyExcelTemp {

    /**
     * 文件名称
     * @return String
     */
    @AliasFor(annotation = EasyExcel.class,attribute = "fileName")
    String fileName();
}
  • 使用方法

@EasyExcelTemp(fileName = "自定义注解导出")
@GetMapping("/run4/{size}")
public List<UseExcel> run4(@PathVariable Integer size) {
    List<UseExcel> useUserExcelList = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        UseExcel useExcel = new UseExcel();
        useExcel.setCurrentTime(LocalDateTime.MAX);
        useExcel.setDesc("自定义注解导出");
        useExcel.setId(i);
        useExcel.setType("自定义注解导出");
        useUserExcelList.add(useExcel);
    }
    return useUserExcelList;
}

excel_145">新增垂直合并excel

@Data
@EasySmart(perfectTable = true)
public class UseExcel {

@EasyExcelFiled(name = "原生注解-id")
@JSONField(name = "JSONField注解-id")
private Integer excelId;

@EasySmartField(name = "`current_time`")
@EasyExcelFiled(name = "原生注解-当前时间")
@JSONField(name = "JSONField注解-当前时间")
private LocalDateTime currentTime;

@EasySmartField(name = "`orderByDesc`")
@EasyExcelFiled(name = "原生注解-描述",fieldMerge = EasyExcelFiled.EasyExcelFieldMerge.VERTICAL)
@JSONField(name = "JSONField注解-描述")
private String desc;

@EasyExcelFiled(name = "原生注解-类型",fieldMerge = EasyExcelFiled.EasyExcelFieldMerge.VERTICAL)
@JSONField(name = "JSONField注解-类型")
private String type;

@EasyExcelFiled(name = "原生注解-是否删除")
@JSONField(name = "JSONField注解-是否删除")
private boolean isDelete;
}
新增Excel 导入支持时间格式
新增注解 @EasyFile 支持导出字符串、文件到mvc框架二进制流文件
支持对象中含有对象
支持对象中含有集合导出自动合并集合

http://www.niftyadmin.cn/n/5403249.html

相关文章

同余的性质

同余的性质 同余的性质1、性质一2、性质二3、性质三4、性质四5、性质五6、性质六7、性质七8、性质八 同余的性质 此处的 d 为最大公约数 \textcolor{red}{此处的d为最大公约数} 此处的d为最大公约数 1、性质一 若 a 1 ≡ b 1 ( m o d a_1\equiv b_1(mod a1​≡b1​(mod m ) …

数据挖掘:航空公司的客户价值分析

需求分析 理解并掌握聚类分析方法&#xff0c;掌握数据的标准化&#xff0c;掌握寻找最佳聚类数&#xff0c;掌握聚类的绘图&#xff0c;掌握聚类分析的应用场景。 系统实现 实验流程分析 借助航空公司数据&#xff0c;对客户进行分类对不同类别的客户进行特征分析&#xf…

python 基础绘图函数 实例

简介 在 Python 中&#xff0c;有许多用于绘图的库。以下是一些常用的 Python 绘图库及其基本绘图函数的简要介绍&#xff1a; Matplotlib: matplotlib.pyplot.plot(x, y): 绘制线图。matplotlib.pyplot.scatter(x, y): 绘制散点图。matplotlib.pyplot.bar(x, height): 绘制条…

【SQL】619. 只出现一次的最大数字 (知识点学习:mysql order by 多个字段排序)

前述 推荐阅读&#xff1a; mysql order by 多个字段排序 MySql Order By 多个字段 排序规则 引用mysql order by 多个字段排序文章中的总结&#xff1a; &#xff08;1&#xff09;在MySql中&#xff0c;使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列。 &#…

Springboot之集成MongoDB无认证与开启认证的配置方式

Springboot之集成MongoDB无认证与开启认证的配置方式 文章目录 Springboot之集成MongoDB无认证与开启认证的配置方式1. application.yml中两种配置方式1. 无认证集成yaml配置2. 有认证集成yaml配置 2. 测试1. 实体类2. 单元测试3. 编写Controller测试 1. application.yml中两种…

关于synchronized介绍

synchronized的特性 1. 乐观锁/悲观锁自适应,开始时是乐观锁,如果锁冲突频繁,就转换为悲观锁 2.轻量级/重量级锁自适应 开始是轻量级锁实现,如果锁被持有的时间较长,就转换成重量级锁 3.自旋/挂起等待锁自适应 4.不是读写锁 5.非公平锁 6,可重入锁 synchronized的使用 1&#…

Sora背后的技术原理:深度探索Video Compression Network与语言理解在视频生成中的应用

Sora背后的技术原理&#xff1a;深度探索Video Compression Network与语言理解在视频生成中的应用 摘要&#xff1a; 随着人工智能技术的飞速发展&#xff0c;视频生成技术逐渐成为研究热点。Sora作为一种先进的视频生成技术&#xff0c;其背后的技术原理值得深入研究。本文详…

PoC免写攻略

在网络安全领域&#xff0c;PoC&#xff08;Proof of Concept&#xff09;起着重要的作用&#xff0c;并且在安全研究、漏洞发现和漏洞利用等方面具有重要的地位。攻击方视角下&#xff0c;常常需要围绕 PoC 做的大量的工作。常常需要从手动测试开始编写 PoC&#xff0c;再到实…