使用的是poi方式生成Excel表,具体使用文档可以在: 链接: https://hutool.cn/docs/#/poi/Excel%E5%B7%A5%E5%85%B7-ExcelUtil.查看
导出接口
@GetMapping("/export")
public void export(HttpServletResponse response) throws Exception{
List<Teacher> list = teacherJobService.list();
ExcelWriter writer = ExcelUtil.getWriter(true);
writer.write(list,true);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
String fileName = URLEncoder.encode("教师信息","UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
ServletOutputStream out = response.getOutputStream();
writer.flush(out,true);
out.close();
writer.close();
}
导入接口
@PostMapping("/import")
public Result imp(MultipartFile file) throws Exception{
InputStream inputStream = file.getInputStream();
ExcelReader reader = ExcelUtil.getReader(inputStream);
List<Teacher> list = reader.readAll(Teacher.class);
System.out.println(list);
userService.saveBatch(list);
return Result.success(true);
}
|