EasyExcel
导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
文件上传
@PostMapping("/api/fileupload/filebatch.do")
@ResponseBody
public R save(MultipartFile file) throws IOException {
if (!file.isEmpty()) {
//获取文件名称
String fn = file.getOriginalFilename();
System.out.println(fn);
if (fn.endsWith("xls") || fn.endsWith(".xlsx")) {
//
List<Test> list = EasyExcel.read(file.getInputStream(), Test.class, null).sheet("Sheet1").doReadSync();
list.stream().forEach(e-> System.out.println(e));
System.out.println("批量导入:" + list.size());
//操作数据库 实现批量新增
return R.setOK(list);
}
}
return R.setERROR("亲,请上传内容,格式只支持:excel表格");
}
文件下载
//文件下载 导出全部的数据
@GetMapping("/api/fileupload/fileexport.do")
public void download(HttpServletResponse response) throws IOException {
//1.设置下载的响应头信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("测试" + System.currentTimeMillis(), "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//2.查询要下载的数据
List<Student> list = studentService.list();
//3.生成数据
EasyExcel.write(response.getOutputStream(), Student.class).sheet("导出用户" + System.currentTimeMillis()).
doWrite(list);
}