一、结果展示
主要功能:可以根据前端传递的参数,导出指定列、指定行
1.1 案例一
- 前端页面
- 传递参数
json
{
"excelName": "导出用户信息1725738666946",
"sheetName": "导出用户信息",
"fieldList": [
{
"fieldName": "userId",
"fieldDesc": "用户id"
},
{
"fieldName": "age",
"fieldDesc": "年龄"
},
{
"fieldName": "address",
"fieldDesc": "地址"
}
]
}
- 导出的文件
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"
]
}
- 导出结果
二、代码实现
工具类代码结构
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) {...}
}