@PostMapping("/person")
public void download(@RequestBody Person person,
HttpServletResponse response){
try {
response.reset();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
String fileName ="学生"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
response.setHeader("Content-disposition",
"attachment;filename=" + URLEncoder.encode(fileName,"utf-8")+".xlsx");
ExcelWriter build = EasyExcelFactory.write()
.file(response.getOutputStream())
.head(PersonExcelVO.class)
.build();
WriteSheet writeSheet = new WriteSheet();
writeSheet.setSheetName("sheet1");
Integer count = personMapper.selectWhiteListCount(person.getName());
if (count != null && count > 0) {
int pageSize = 50000;
int total = (int) Math.ceil((double) count / pageSize);
for (int i = 0; i < total; i++) {
List<Person> persons=
personMapper.selectStudent(person.getName(),
i * pageSize,
pageSize);
if (CollectionUtils.isNotEmpty(persons)) {
build.write(PersonMapping.INSTANCE.convertToList(persons), writeSheet);
}
}
} else {
build.write(Collections.emptyList(), writeSheet);
}
build.close();
build.finish();
}catch (Exception e){
log.error("download person error",e);
}
}
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.3</version>
<scope>provided</scope>
</dependency>
@GetMapping("/download")
public void productTemplateDownload(HttpServletResponse response) {
log.info("product template start====");
OutputStream outputStream = null;
InputStream inputStream = null;
try {
String fileName ="product";
File file = new File(filePath +"product.xlsx" );
inputStream = Files.newInputStream(file.toPath());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"utf-8")+".xlsx");
// 获取输出流
outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
log.info("product template end====");
} catch (IOException e) {
log.error("文件下载出错", e);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
}