@RequestMapping("/export") public void export(HttpServletResponse response){ try { List<User> list=userMapper.findAll(); ExcelWriter excelWriter= ExcelUtil.getWriter(true); excelWriter.write(list,true); //response为HttpServletResponse对象 response.setContentType("application/vnd.ms-excel;charset=utf-8"); //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码 String filename= null; filename = URLEncoder.encode("用户信息","UTF-8"); response.setHeader("Content-Disposition","attachment;filename="+filename+".xls"); ServletOutputStream out = response.getOutputStream(); excelWriter.flush(out, true); // 关闭writer,释放内存 excelWriter.close(); //此处记得关闭输出Servlet流 IoUtil.close(out); } catch (Exception e){ e.printStackTrace(); } } @RequestMapping("/import") public Boolean imports(MultipartFile file){ try { InputStream inputStream=file.getInputStream(); ExcelReader excelReader=ExcelUtil.getReader(inputStream); List<User> list=excelReader.readAll(User.class); for (User u : list) { userMapper.addUser(u); } System.out.println(list); return true; } catch (IOException e) { e.printStackTrace(); } return false; }
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.23</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
标签:导出,list,filename,导入,excelWriter,excle,true,response,out From: https://www.cnblogs.com/wllovelmbforever/p/17892840.html