springboot版本3.2.0,数据库版本8
mybatisplus版本3.5.4.1
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.3</version></dependency>
实体类中的枚举类型转换,因调用方法后还是不能转换类型所以暂未解决
public class User {
@ExcelProperty("用户编号")
@ColumnWidth(20)
private Long id;
@ExcelProperty("姓名")
@ColumnWidth(20)
private String username;
@ExcelProperty("密码")
@ColumnWidth(20)
private String password;
@ExcelProperty("用户电话")
@ColumnWidth(20)
private String phone;
@ExcelProperty("用户信息")
@ColumnWidth(20)
private String info;
//@ExcelProperty(value = "用户状态",converter = UserStatusConverter.class)
@ExcelIgnore
@ColumnWidth(20)
private UserStatus status;
@ExcelProperty("用户金额")
@ColumnWidth(20)
private Integer balance;
@ExcelProperty("用户创建时间")
@ColumnWidth(20)
private LocalDateTime createTime;
@ExcelProperty("用户更新时间")
@ColumnWidth(20)
private LocalDateTime updateTime;
@GetMapping("/user")
public void exportUserExcel(HttpServletResponse response){
try{
this.setExcelResponseProp(response,"用户列表");
List<User> userList =userService.listAllUsers(new User());
EasyExcel.write(response.getOutputStream())
.head(User.class)
.excelType(ExcelTypeEnum.XLS)
.sheet("用户列表")
.doWrite(userList);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* postman测试
* @param response
* @param rawFileName
* @throws UnsupportedEncodingException
*/
private void setExcelResponseProp(HttpServletResponse response,String rawFileName) throws UnsupportedEncodingException{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
}
获取数据成功