本文主要是介绍SpringBoot中利用EasyExcel+aop实现一个通用Excel导出功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、结果展示
主要功能:可以根据前端传递的参数,导出指定列、指定行
1.1 案例一
- 前端页面
- 传递参数
{"excelName": "导出用户信息1725738666946","sheetName": "导出用户信息","fieldList": [{"fieldName": "userId","fieldDesc": "用户id"},{"fieldName": "age","fieldDesc": "年龄"},{"fieldName": "address","fieldDesc": "地址"}]
}
- 导出的文件
1.2 案例二
-
前端页面
-
传递参数
{"excelName": "导出用户信息1725739080367","sheetName": "导出用户信息","fieldList": [{"fieldName": "userId","fieldDesc": "用户id"},{"fieldName": "address","fieldDesc": "地址"},{"fieldName": "age","fieldDesc": "年龄"},{"fieldName": "userName","fieldDesc": "用户名"}],"userIdList": ["1","3","5","8","9"]
}
- 导出结果
二、代码实现
工具类代码结构
2.1 入参、出参
UserExportRequest
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
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
这篇关于SpringBoot中利用EasyExcel+aop实现一个通用Excel导出功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!