经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
springboot+easypoi 一行代码搞定excel导入导出
来源:cnblogs  作者:追风的蚂蚁  时间:2019/10/29 11:27:15  对本文有异议

easyPoi 官方API

http://easypoi.mydoc.io/

pom引入

  1. <dependency>
  2.   <groupId>cn.afterturn</groupId>
  3.   <artifactId>easypoi-base</artifactId>
  4.   <version>3.0.3</version>
  5. </dependency>
  6. <dependency>
  7.   <groupId>cn.afterturn</groupId>
  8.   <artifactId>easypoi-web</artifactId>
  9.   <version>3.0.3</version>
  10. </dependency>
  11. <dependency>
  12.   <groupId>cn.afterturn</groupId>
  13.   <artifactId>easypoi-annotation</artifactId>
  14.   <version>3.0.3</version>
  15. </dependency>

实体类

  1. @Data
  2. public class Person {
  3. @Excel(name = "姓名", orderNum = "0")
  4. private String name;
  5. @Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "1")
  6. private String sex;
  7. @Excel(name = "生日", exportFormat = "yyyy-MM-dd", orderNum = "2")
  8. private Date birthday;
  9. public Person(String name, String sex, Date birthday) {
  10. this.name = name;
  11. this.sex = sex;
  12. this.birthday = birthday;
  13. }
  14. }


导入导出公共方法

  1. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,boolean isCreateHeader, HttpServletResponse response){
  2. ExportParams exportParams = new ExportParams(title, sheetName);
  3. exportParams.setCreateHeadRows(isCreateHeader);
  4. defaultExport(list, pojoClass, fileName, response, exportParams);
  5. }
  6. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){
  7. defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
  8. }
  9. public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
  10. defaultExport(list, fileName, response);
  11. }
  12. private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
  13. Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
  14. if (workbook != null);
  15. downLoadExcel(fileName, response, workbook);
  16. }
  17. private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
  18. try {
  19. response.setCharacterEncoding("UTF-8");
  20. response.setHeader("content-Type", "application/vnd.ms-excel");
  21. response.setHeader("Content-Disposition",
  22. "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  23. workbook.write(response.getOutputStream());
  24. } catch (IOException e) {
  25. throw new NormalException(e.getMessage());
  26. }
  27. }
  28. private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
  29. Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
  30. if (workbook != null);
  31. downLoadExcel(fileName, response, workbook);
  32. }
  33. public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){
  34. if (StringUtils.isBlank(filePath)){
  35. return null;
  36. }
  37. ImportParams params = new ImportParams();
  38. params.setTitleRows(titleRows);
  39. params.setHeadRows(headerRows);
  40. List<T> list = null;
  41. try {
  42. list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
  43. }catch (NoSuchElementException e){
  44. throw new NormalException("模板不能为空");
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. throw new NormalException(e.getMessage());
  48. }
  49. return list;
  50. }
  51. public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
  52. if (file == null){
  53. return null;
  54. }
  55. ImportParams params = new ImportParams();
  56. params.setTitleRows(titleRows);
  57. params.setHeadRows(headerRows);
  58. List<T> list = null;
  59. try {
  60. list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
  61. }catch (NoSuchElementException e){
  62. throw new NormalException("excel文件不能为空");
  63. } catch (Exception e) {
  64. throw new NormalException(e.getMessage());
  65. }
  66. return list;
  67. }


测试

  1. @RequestMapping("export")
  2. public void export(HttpServletResponse response){
  3. //模拟从数据库获取需要导出的数据
  4. List<Person> personList = new ArrayList<>();
  5. Person person1 = new Person("路飞","1",new Date());
  6. Person person2 = new Person("娜美","2", DateUtils.addDate(new Date(),3));
  7. Person person3 = new Person("索隆","1", DateUtils.addDate(new Date(),10));
  8. Person person4 = new Person("小狸猫","1", DateUtils.addDate(new Date(),-10));
  9. personList.add(person1);
  10. personList.add(person2);
  11. personList.add(person3);
  12. personList.add(person4);
  13. //导出操作
  14. FileUtil.exportExcel(personList,"花名册","草帽一伙",Person.class,"海贼王.xls",response);
  15. }
  16. @RequestMapping("importExcel")
  17. public void importExcel(){
  18. String filePath = "F:\\海贼王.xls";
  19. //解析excel,
  20. List<Person> personList = FileUtil.importExcel(filePath,1,1,Person.class);
  21. //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
  22. System.out.println("导入数据一共【"+personList.size()+"】行");
  23. //TODO 保存数据库
  24. }


————————————————

原文链接:https://blog.csdn.net/zhefudexiaojiahuo/article/details/83586498

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号