1、导入pom依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
先实现导出:
2、Controller类
@ApiOperation(value = "通过uuid导出")
@GetMapping(value = "/{uuid}")
public void exportExcel(@PathVariable("uuid") String uuid,
HttpServletResponse response) throws IOException {
try {
//导出的文件名前缀,尽量不要有中文
String prefix = "Aps";
//导出的文件名前缀中的日期
String today = new SimpleDateFormat("-yyyy-MM-dd").format(DateUtils.StartOfToday());
//拼在一起,导出为Aps-2021-08-26.xlsx
String filename = prefix.concat(today);
//MSEXCEL格式
response.setContentType("application/vnd.ms-excel");
//指定的类型是文件的扩展名xlsx
response.setHeader("Content-disposition",
"attachment; filename=" + filename + ExcelTypeEnum.XLSX.getValue());
//实现方法,需要加入到excel中的数据
apsService.writeToExcel(uuid, response.getOutputStream());
} catch