springboot easy-excel下载
·
@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);
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)