SpringBoot中利用EasyExcel+aop实现一个通用Excel导出功能

一、结果展示

主要功能:可以根据前端传递的参数,导出指定列、指定行

1.1 案例一

  1. 前端页面
  2. 传递参数
json 复制代码
{
    "excelName": "导出用户信息1725738666946",
    "sheetName": "导出用户信息",
    "fieldList": [
        {
            "fieldName": "userId",
            "fieldDesc": "用户id"
        },
        {
            "fieldName": "age",
            "fieldDesc": "年龄"
        },
        {
            "fieldName": "address",
            "fieldDesc": "地址"
        }
    ]
}
  1. 导出的文件

1.2 案例二

  1. 前端页面

  2. 传递参数

json 复制代码
{
    "excelName": "导出用户信息1725739080367",
    "sheetName": "导出用户信息",
    "fieldList": [
        {
            "fieldName": "userId",
            "fieldDesc": "用户id"
        },
        {
            "fieldName": "address",
            "fieldDesc": "地址"
        },
        {
            "fieldName": "age",
            "fieldDesc": "年龄"
        },
        {
            "fieldName": "userName",
            "fieldDesc": "用户名"
        }
    ],
    "userIdList": [
        "1",
        "3",
        "5",
        "8",
        "9"
    ]
}
  1. 导出结果

二、代码实现

工具类代码结构

2.1 入参、出参

UserExportRequest

java 复制代码
public class UserExportRequest extends ExcelExportRequest {
    /**
     * 要导出的用户id列表,不传,则导出所有用户记录
     */
    private List<Integer> userIdList;

    public List<Integer> getUserIdList() {...}
    public void setUserIdList(List<Integer> userIdList) {...}
}

---继承自通用的ExcelExportRequest类---
public class ExcelExportRequest {
    /**
     * excel名称
     */
    private String excelName;

    /**
     * sheet的名称
     */
    private String sheetName;

    /**
     * 导出字段有序列表
     */
    private List<ExcelExportField> fieldList;

    public String getSheetName() {...}
    public void setSheetName(String sheetName) {...}
    public String getExcelName() {...}
    public void setExcelName(String excelName) {...}
    public List<ExcelExportField> getFieldList() {...}
    public void setFieldList(List<ExcelExportField> fieldList) {...}
}

ExcelExportResponse

java 复制代码
public class ExcelExportResponse {
    //导出的excel文件名称
    private String excelName;
    // sheet列表数据
    private List<ExcelSheet> sheetList;

    public String getExcelName() {...}
    public void setExcelName(String excelName) {...}
    public List<ExcelSheet> getSheetList() {...}
    public void setSheetList(List<ExcelSheet> sheetList) {...}
}

2.2 生成ExcelExportResponse的逻辑

2.3 aop通知

2.4 下载Excel

相关推荐
明月_清风6 小时前
Muse 登顶 App Store 第一,SDK 直接开源:AI Agent 开始进入下一个阶段
人工智能·后端
hsfxuebao9 小时前
Loop Engineering 保姆级教程 + 项目实战
人工智能·后端
打工仔折腾 AI9 小时前
从 Demo 到生产级 Agent:8 个关键设计机制与 Python 实现拆解
java·jvm·人工智能·后端·python·langchain·ai agent 实战
架构技术专栏10 小时前
交叉熵:AI 怎样给概率预测打分
后端
code_slave(码畜)10 小时前
微服务架构落地:基础服务 —— 报表服务(中篇:元数据、查询引擎、缓存与权限落地)
spring boot·spring cloud·缓存·微服务·架构
ZealSinger11 小时前
Boot4挂起函数丢traceId怎么修
spring boot·kotlin·协程·可观测性
IT_陈寒11 小时前
SpringBoot自动配置坑了我一把,原来是这样绕过去的
前端·人工智能·后端
code_slave(码畜)12 小时前
微服务架构落地:基础服务 —— 报表服务(AI 集成篇:AI 增强报表能力)
人工智能·spring boot·spring cloud·微服务·架构
要努力啊46912 小时前
用 Codex 加速 Java 开发:从代码生成到测试覆盖的完整实战
后端
dora12 小时前
LangChain4j 新手入门实战教程(Java版)
后端·langchain·agent