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;
Controller层
@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");
}
postman测试
Excel表格
获取数据成功文章来源:https://www.toymoban.com/news/detail-802639.html
文章来源地址https://www.toymoban.com/news/detail-802639.html
到了这里,关于Springboot通过前端发起请求,拿到数据库中的数据并生成excel表格,postman请求并下载文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!