SpringBoot+ EasyExcel实现Excel文件的导入导出
程序员文章站
2024-03-21 11:57:34
...
SpringBoot+ EasyExcel实现Excel文件的导入导出
##1.引入相关依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>
2.controller
@GetMapping("/excel")
public void export(HttpServletResponse response,QueryRequest request, Certificate certificate) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = null;
try {
fileName = URLEncoder.encode(UUID.randomUUID().toString(), "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
IPage<List<Certificate>> certificate1 = this.certificateService.findCertificate(request, certificate);
List<List<Certificate>> records = certificate1.getRecords();//查出来的list 对象
EasyExcel.write(response.getOutputStream(), Certificate.class).autoCloseStream(Boolean.FALSE).sheet("模板").doWrite(records);
} catch (Exception e) {
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> map = new HashMap<String, String>();
map.put("status", "failure");
map.put("message", "下载文件失败" + e.getMessage());
try {
response.getWriter().println(JSON.toJSONString(map));
} catch (IOException ioException) {
ioException.printStackTrace();
}
e.printStackTrace();
log.error("下载失败"+e);
}
}
```就ok了
推荐阅读
-
SpringBoot+ EasyExcel实现Excel文件的导入导出
-
springboot框架中-java实现Excel、csv、txt 文件的批量导出数据-亲手实测(二)-csv & txt
-
利用EasyExcel将数据导出或导入成Excel格式文件
-
Java代码实现excel的导入和导出
-
用EasyExcel实现表格的导入,导出
-
文件EasyExcel的导入和导出
-
掌财社:Java如何使用EasyExcel实现导入导出excel
-
java使用阿里的easyExcel实现Excel导出功能
-
详解thinkphp实现excel数据的导入导出(附完整案例)
-
Java POI实现将导入Excel文件的示例代码