ExcelHelper.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package jnpf.excel;
  2. import cn.afterturn.easypoi.excel.entity.ExportParams;
  3. import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
  4. import jnpf.excel.handle.ExcelCommentHandle;
  5. import jnpf.excel.handle.ExcelDataValidation;
  6. import jnpf.excel.handle.ExcelRequireRedColor;
  7. import jnpf.model.ExcelColumnAttr;
  8. import jnpf.model.ExcelModel;
  9. import jnpf.util.ExcelUtil;
  10. import lombok.Data;
  11. import lombok.NoArgsConstructor;
  12. import org.apache.commons.collections4.CollectionUtils;
  13. import org.apache.poi.ss.usermodel.*;
  14. import org.apache.poi.xssf.usermodel.XSSFCell;
  15. import java.util.*;
  16. import java.util.stream.Collectors;
  17. @Data
  18. @NoArgsConstructor
  19. public class ExcelHelper {
  20. // 是否生成示例数据
  21. public boolean needExampleData = false;
  22. boolean initPre = false;
  23. boolean initPost = false;
  24. /**
  25. * 控件信息
  26. */
  27. List<ExcelColumnAttr> models;
  28. /**
  29. * 控件信息和excel导出的key对应关系
  30. */
  31. Map<String, ExcelColumnAttr> modelMap = new HashMap<>();
  32. /**
  33. * excel参数
  34. */
  35. ExportParams exportParams;
  36. /**
  37. * 导出字段信息
  38. */
  39. List<ExcelExportEntity> entities;
  40. private Workbook workbook;
  41. private List<ExcelFunction> preHandleFunctions = new ArrayList<>();
  42. private List<ExcelFunction> postHandleFunctions = new ArrayList<>();
  43. private Map<String, String[]> optionMap;
  44. /**
  45. * 额外参数
  46. */
  47. private Map<String, Object> extraParams = new HashMap<>();
  48. /**
  49. * 存在子表
  50. */
  51. private boolean complexTable;
  52. private void preDataHandle() {
  53. for (ExcelExportEntity entity : this.entities) {
  54. if (CollectionUtils.isNotEmpty(entity.getList())) {
  55. this.complexTable = true;
  56. break;
  57. }
  58. }
  59. // 控件映射
  60. for (ExcelColumnAttr model : this.models) {
  61. String key = model.getKey();
  62. modelMap.put(key, model);
  63. }
  64. }
  65. public void init(Workbook workbook, ExportParams exportParams, List<ExcelExportEntity> entities, ExcelModel excelModel) {
  66. this.workbook = workbook;
  67. this.exportParams = exportParams != null ? exportParams : new ExportParams();
  68. this.models = excelModel.getModels() != null ? excelModel.getModels() : new ArrayList<>();
  69. this.entities = entities != null ? entities : new ArrayList<>();
  70. this.optionMap = excelModel.getOptionMap() != null ? excelModel.getOptionMap() : new HashMap<>();
  71. this.preDataHandle();
  72. }
  73. private void initPreHandle() {
  74. addPreHandle(new ExcelCommentHandle()::execute);
  75. this.initPre = true;
  76. }
  77. private void initPostHandle() {
  78. addPostHandle(new ExcelDataValidation()::execute);
  79. addPostHandle(new ExcelRequireRedColor()::execute);
  80. this.initPost = true;
  81. }
  82. public void doPreHandle() {
  83. if (!initPre) {
  84. this.initPreHandle();
  85. }
  86. addPreHandle(new ExcelCommentHandle()::execute);
  87. preHandleFunctions.stream().filter(Objects::nonNull).collect(Collectors.toList()).forEach(item -> item.apply(this, this.extraParams));
  88. }
  89. public void doPostHandle() {
  90. if (!initPost) {
  91. this.initPostHandle();
  92. }
  93. postHandleFunctions.stream().filter(Objects::nonNull).collect(Collectors.toList()).forEach(item -> item.apply(this, this.extraParams));
  94. // 移除标题的括号文字
  95. this.removeTitleConclusion();
  96. // 添加border线
  97. this.addCellBorder();
  98. }
  99. private void addCellBorder() {
  100. Sheet sheet = workbook.getSheet(exportParams.getSheetName());
  101. int rowLen = sheet.getLastRowNum();
  102. int startRowLen = complexTable ? 2 : 1;
  103. int lastCellNum = sheet.getRow(startRowLen - 1).getLastCellNum();
  104. CellStyle style = workbook.createCellStyle();
  105. ExcelExportStyler excelExportStyler = new ExcelExportStyler(workbook);
  106. for (int i = startRowLen; i <= rowLen; i++) {
  107. Row row = sheet.getRow(i);
  108. for (int j = 0; j < lastCellNum; j++) {
  109. Cell cell = row.getCell(j);
  110. if (cell == null) cell = row.createCell(j);
  111. excelExportStyler.setBlackBorder(style);
  112. //设置单元格为文本格式
  113. DataFormat dataFormat = workbook.createDataFormat();
  114. style.setDataFormat(dataFormat.getFormat("@"));
  115. cell.setCellStyle(style);
  116. }
  117. }
  118. }
  119. private void removeTitleConclusion() {
  120. Sheet sheet = workbook.getSheet(exportParams.getSheetName());
  121. int rowLen = complexTable ? 2 : 1;
  122. for (int i = 0; i < rowLen; i++) {
  123. Row headerRow = sheet.getRow(i);
  124. int lastCellNum = headerRow.getLastCellNum();
  125. for (int j = 0; j < lastCellNum; j++) {
  126. Cell cell = headerRow.getCell(j);
  127. if (cell == null) continue;
  128. String name = ((XSSFCell) cell).getRichStringCellValue().getString();
  129. if (ExcelUtil.matcherFind(name)) {
  130. ((XSSFCell) cell).getRichStringCellValue().setString(name.substring(0, name.lastIndexOf("(")));
  131. }
  132. }
  133. }
  134. }
  135. public void addPreHandle(ExcelFunction... functions) {
  136. Collections.addAll(preHandleFunctions, functions);
  137. }
  138. public void addPostHandle(ExcelFunction... functions) {
  139. Collections.addAll(postHandleFunctions, functions);
  140. }
  141. }