CellMergeStrategy.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.ruoyi.common.excel;
  2. import com.alibaba.excel.metadata.Head;
  3. import com.alibaba.excel.write.merge.AbstractMergeStrategy;
  4. import com.ruoyi.common.annotation.CellMerge;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Data;
  7. import lombok.SneakyThrows;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.collections4.CollectionUtils;
  10. import org.apache.poi.ss.usermodel.Cell;
  11. import org.apache.poi.ss.usermodel.Sheet;
  12. import org.apache.poi.ss.util.CellRangeAddress;
  13. import java.lang.reflect.Field;
  14. import java.lang.reflect.Method;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * 列值重复合并策略
  21. *
  22. * @author Lion Li
  23. */
  24. @AllArgsConstructor
  25. @Slf4j
  26. public class CellMergeStrategy extends AbstractMergeStrategy {
  27. private List<?> list;
  28. private boolean hasTitle;
  29. @Override
  30. protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
  31. List<CellRangeAddress> cellList = handle(list, hasTitle);
  32. // judge the list is not null
  33. if (CollectionUtils.isNotEmpty(cellList)) {
  34. // the judge is necessary
  35. if (cell.getRowIndex() == 1 && cell.getColumnIndex() == 0) {
  36. for (CellRangeAddress item : cellList) {
  37. sheet.addMergedRegion(item);
  38. }
  39. }
  40. }
  41. }
  42. @SneakyThrows
  43. private static List<CellRangeAddress> handle(List<?> list, boolean hasTitle) {
  44. List<CellRangeAddress> cellList = new ArrayList<>();
  45. if (CollectionUtils.isEmpty(list)) {
  46. return cellList;
  47. }
  48. Class<?> clazz = list.get(0).getClass();
  49. Field[] fields = clazz.getDeclaredFields();
  50. // 有注解的字段
  51. List<Field> mergeFields = new ArrayList<>();
  52. List<Integer> mergeFieldsIndex = new ArrayList<>();
  53. for (int i = 0; i < fields.length; i++) {
  54. Field field = fields[i];
  55. if (field.isAnnotationPresent(CellMerge.class)) {
  56. CellMerge cm = field.getAnnotation(CellMerge.class);
  57. mergeFields.add(field);
  58. mergeFieldsIndex.add(cm.index() == -1 ? i : cm.index());
  59. }
  60. }
  61. // 行合并开始下标
  62. int rowIndex = hasTitle ? 1 : 0;
  63. Map<Field, RepeatCell> map = new HashMap<>();
  64. // 生成两两合并单元格
  65. for (int i = 0; i < list.size(); i++) {
  66. for (int j = 0; j < mergeFields.size(); j++) {
  67. Field field = mergeFields.get(j);
  68. String name = field.getName();
  69. String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
  70. Method readMethod = clazz.getMethod(methodName);
  71. Object val = readMethod.invoke(list.get(i));
  72. int colNum = mergeFieldsIndex.get(j);
  73. if (!map.containsKey(field)) {
  74. map.put(field, new RepeatCell(val, i));
  75. } else {
  76. RepeatCell repeatCell = map.get(field);
  77. Object cellValue = repeatCell.getValue();
  78. if (cellValue == null || "".equals(cellValue)) {
  79. // 空值跳过不合并
  80. continue;
  81. }
  82. if (!cellValue.equals(val)) {
  83. if (i - repeatCell.getCurrent() > 1) {
  84. cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum));
  85. }
  86. map.put(field, new RepeatCell(val, i));
  87. } else if (i == list.size() - 1) {
  88. if (i > repeatCell.getCurrent()) {
  89. cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return cellList;
  96. }
  97. @Data
  98. @AllArgsConstructor
  99. static class RepeatCell {
  100. private Object value;
  101. private int current;
  102. }
  103. }