Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

132 righe
5.2 KiB

  1. 2.4 注解变种-更自由的导出
  2. ===
  3. 这天老师又把路飞喊道的办公室,要求路飞导出班级学生的整体信息
  4. ```java
  5. @Excel(name = "学生姓名", height = 20, width = 30, isImportField = "true_st")
  6. private String name;
  7. @Excel(name = "学生性别", replace = { "男_1", "女_2" }, suffix = "生", isImportField = "true_st")
  8. private int sex;
  9. @Excel(name = "出生日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd", isImportField = "true_st", width = 20)
  10. private Date birthday;
  11. @Excel(name = "进校日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd")
  12. private Date registrationDate;
  13. ```
  14. 路飞飞快的用到上面的学到的知识搞定了,这这时有一个老师把路飞叫去,说想要导出一个不要出生日期的Excel,感觉用户需求很无奈,路飞又造两个一个bean,把这个注解去掉了,来导出
  15. ```java
  16. @Excel(name = "学生姓名", height = 20, width = 30, isImportField = "true_st")
  17. private String name;
  18. @Excel(name = "学生性别", replace = { "男_1", "女_2" }, suffix = "生", isImportField = "true_st")
  19. private int sex;
  20. @Excel(name = "进校日期", databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd")
  21. private Date registrationDate;
  22. ```
  23. 虽然解决了老师的需求,但这个并不是一个完美的解决方案,下面介绍一个更自由的解决方案
  24. 注解的导出,规定我们必须把model写好,并且注解写好,每次导出的Excel都是固定的,无法动态控制导出的列,虽然可以通过id来处理一个案例,但是自由度远远不够,这里介绍个变种支持,基本支持注解所有的功能
  25. 基于List<ExcelExportEntity> 的导出,ExcelExportEntity是注解经过处理翻译成的实体类,两者几乎是一对的,所以如果我们要动态自定义导出列,我们只要动态拼装ExcelExportEntity就可以了
  26. 下面我们看下这个类
  27. ```java
  28. /**
  29. * 如果是MAP导出,这个是map的key
  30. */
  31. private Object key;
  32. private double width = 10;
  33. private double height = 10;
  34. /**
  35. * 图片的类型,1是文件,2是数据库
  36. */
  37. private int exportImageType = 0;
  38. /**
  39. * 排序顺序
  40. */
  41. private int orderNum = 0;
  42. /**
  43. * 是否支持换行
  44. */
  45. private boolean isWrap;
  46. /**
  47. * 是否需要合并
  48. */
  49. private boolean needMerge;
  50. /**
  51. * 单元格纵向合并
  52. */
  53. private boolean mergeVertical;
  54. /**
  55. * 合并依赖
  56. */
  57. private int[] mergeRely;
  58. /**
  59. * 后缀
  60. */
  61. private String suffix;
  62. /**
  63. * 统计
  64. */
  65. private boolean isStatistics;
  66. private String numFormat;
  67. private List<ExcelExportEntity> list;
  68. ```
  69. 基本上是和注解对应的, **List<ExcelExportEntity> list 这个是对应的一对多的导出,相当于集合,其他基本上都是和注解保持一致**
  70. 下面给出正常的demo
  71. ```java
  72. public void test() {
  73. try {
  74. List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
  75. //构造对象等同于@Excel
  76. ExcelExportEntity excelentity = new ExcelExportEntity("姓名", "name");
  77. excelentity.setNeedMerge(true);
  78. entity.add(excelentity);
  79. entity.add(new ExcelExportEntity("性别", "sex"));
  80. excelentity = new ExcelExportEntity(null, "students");
  81. List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>();
  82. temp.add(new ExcelExportEntity("姓名", "name"));
  83. temp.add(new ExcelExportEntity("性别", "sex"));
  84. //构造List等同于@ExcelCollection
  85. excelentity.setList(temp);
  86. entity.add(excelentity);
  87. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  88. //把我们构造好的bean对象放到params就可以了
  89. Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("测试", "测试"), entity,
  90. list);
  91. FileOutputStream fos = new FileOutputStream("D:/excel/ExcelExportForMap.tt.xls");
  92. workbook.write(fos);
  93. fos.close();
  94. } catch (FileNotFoundException e) {
  95. e.printStackTrace();
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. ```
  101. 路飞想到了这个方案,并且用上面做了测试可以完美解决所以他把之前的代码改为了(**代码有删减,基本上都是和注解对应的**)
  102. ```java
  103. List<ExcelExportEntity> beanList = new ArrayList<ExcelExportEntity>();
  104. beanList .add(new ExcelExportEntity(new ExcelExportEntity("学生姓名", "name"));
  105. beanList .add(new ExcelExportEntity("学生性别", "sex"));
  106. beanList .add(new ExcelExportEntity("进校日期", "registrationDate"));
  107. if(needBirthday()){
  108. beanList .add(new ExcelExportEntity("出生日期", "birthday"));
  109. }
  110. Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("测试", "测试"), beanList ,
  111. list);
  112. ```
  113. 用同一套代买完美了支持了老师的需求,心满意足的回宿舍了^^