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

相关推荐
qq_2975746721 分钟前
【实战教程】SpringBoot 实现多文件批量下载并打包为 ZIP 压缩包
java·spring boot·后端
tb_first4 小时前
LangChain4j简单入门
java·spring boot·langchain4j
计算机学姐8 小时前
基于SpringBoot的民宿预定管理系统【三角色+个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·mysql·信息可视化·intellij-idea·推荐算法
计算机程序设计小李同学8 小时前
基于 Spring Boot + Vue 的龙虾专营店管理系统的设计与实现
java·spring boot·后端·spring·vue
LiZhen7989 小时前
SpringBoot 实现动态切换数据源
java·spring boot·mybatis
Charlie_lll10 小时前
力扣解题-[3379]转换数组
数据结构·后端·算法·leetcode
qq_124987075310 小时前
基于Java Web的城市花园小区维修管理系统的设计与实现(源码+论文+部署+安装)
java·开发语言·前端·spring boot·spring·毕业设计·计算机毕业设计
VX:Fegn089511 小时前
计算机毕业设计|基于springboot + vue云租车平台系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Chasmれ11 小时前
Spring Boot 1.x(基于Spring 4)中使用Java 8实现Token
java·spring boot·spring
汤姆yu11 小时前
2026基于springboot的在线招聘系统
java·spring boot·后端