springboot版本3.2.0,數(shù)據(jù)庫版本8
mybatisplus版本3.5.4.1
依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.3</version></dependency>
實體類
實體類中的枚舉類型轉(zhuǎn)換,因調(diào)用方法后還是不能轉(zhuǎn)換類型所以暫未解決
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 = "用戶狀態(tài)",converter = UserStatusConverter.class)
@ExcelIgnore
@ColumnWidth(20)
private UserStatus status;
@ExcelProperty("用戶金額")
@ColumnWidth(20)
private Integer balance;
@ExcelProperty("用戶創(chuàng)建時間")
@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表格
獲取數(shù)據(jù)成功文章來源:http://www.zghlxwxcb.cn/news/detail-802639.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-802639.html
到了這里,關于Springboot通過前端發(fā)起請求,拿到數(shù)據(jù)庫中的數(shù)據(jù)并生成excel表格,postman請求并下載文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!