java 基于alibaba EasyExcel 属性注解!快速实现Excel导出功能
前言:我们要对我们查询出来的数据做Excel导出功能,话不多说,撸起袖子干
1.定义实体类对象并加上注解
@Data
public class ExcelTeacherClockIn {
@ExcelProperty(value = "老师" ,index = 0)
private String teacherName;
@ExcelProperty(value = "打卡时间" ,index = 1)
private Date clockInTime;
@ExcelProperty(value = "打卡状态" ,index = 2)
private String clockInStatusStr;
@ExcelProperty(value = "会所" ,index = 3)
private String clubName;
@ExcelProperty(value = "类型" ,index = 4)
private String scheduleTypeStr;
@ExcelProperty(value = "课程" ,index = 5)
private String className;
@ExcelProperty(value = "开始时间" ,index = 6)
private Date startTime;
@ExcelProperty(value = "结束时间" ,index = 7)
private Date endTime;
}
@ExcelProperty(value = “列名” ,index = 列下标)
2.查询出的数据,直接使用官方提供的工具
List<ExcelTeacherClockIn> list = teacherClockInService.getAdminTeacherClockInListExcel(criteria);
ExcelUtilAli.writeExcel(response, list, "老师打卡明细列表", "老师打卡明细列表", ExcelTeacherClockIn.class);
3.前端调用后端接口,要使用流访问接口 responseType: ‘blob’,
我之前就是没加这个,请求成功下载出来的文件一直都是空。这点要注意下
export function getAdminTeacherClockInListExcel(data) {
return request({
url: '/yplushelper/teacherClockIn/getAdminTeacherClockInListExcel',
method: 'get',
responseType: 'blob',
params: data
});
}
请求接口下载文件成功之后,我们打开文件看一下