|
@@ -1,175 +1,59 @@
|
|
|
package com.vber.common.excel.core;
|
|
package com.vber.common.excel.core;
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
-import cn.hutool.core.util.ReflectUtil;
|
|
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
|
|
-import cn.idev.excel.annotation.ExcelIgnore;
|
|
|
|
|
-import cn.idev.excel.annotation.ExcelIgnoreUnannotated;
|
|
|
|
|
-import cn.idev.excel.annotation.ExcelProperty;
|
|
|
|
|
import cn.idev.excel.metadata.Head;
|
|
import cn.idev.excel.metadata.Head;
|
|
|
import cn.idev.excel.write.handler.WorkbookWriteHandler;
|
|
import cn.idev.excel.write.handler.WorkbookWriteHandler;
|
|
|
import cn.idev.excel.write.handler.context.WorkbookWriteHandlerContext;
|
|
import cn.idev.excel.write.handler.context.WorkbookWriteHandlerContext;
|
|
|
import cn.idev.excel.write.merge.AbstractMergeStrategy;
|
|
import cn.idev.excel.write.merge.AbstractMergeStrategy;
|
|
|
-import com.vber.common.core.utils.reflect.ReflectUtils;
|
|
|
|
|
-import com.vber.common.excel.annotation.CellMerge;
|
|
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
|
|
-import lombok.Data;
|
|
|
|
|
-import lombok.SneakyThrows;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
|
|
|
-import java.lang.reflect.Field;
|
|
|
|
|
-import java.util.*;
|
|
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 列值重复合并策略
|
|
* 列值重复合并策略
|
|
|
*
|
|
*
|
|
|
- * @author Iwb
|
|
|
|
|
|
|
+ * @author Lion Li
|
|
|
*/
|
|
*/
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class CellMergeStrategy extends AbstractMergeStrategy implements WorkbookWriteHandler {
|
|
public class CellMergeStrategy extends AbstractMergeStrategy implements WorkbookWriteHandler {
|
|
|
|
|
|
|
|
private final List<CellRangeAddress> cellList;
|
|
private final List<CellRangeAddress> cellList;
|
|
|
- private final boolean hasTitle;
|
|
|
|
|
- private int rowIndex;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public CellMergeStrategy(List<CellRangeAddress> cellList) {
|
|
|
|
|
+ this.cellList = cellList;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
public CellMergeStrategy(List<?> list, boolean hasTitle) {
|
|
public CellMergeStrategy(List<?> list, boolean hasTitle) {
|
|
|
- this.hasTitle = hasTitle;
|
|
|
|
|
- // 行合并开始下标
|
|
|
|
|
- this.rowIndex = hasTitle ? 1 : 0;
|
|
|
|
|
- this.cellList = handle(list, hasTitle);
|
|
|
|
|
|
|
+ this.cellList = CellMergeHandler.of(hasTitle).handle(list);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
|
|
protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
|
|
|
|
|
+ if (CollUtil.isEmpty(cellList)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
//单元格写入了,遍历合并区域,如果该Cell在区域内,但非首行,则清空
|
|
//单元格写入了,遍历合并区域,如果该Cell在区域内,但非首行,则清空
|
|
|
final int rowIndex = cell.getRowIndex();
|
|
final int rowIndex = cell.getRowIndex();
|
|
|
- if (CollUtil.isNotEmpty(cellList)) {
|
|
|
|
|
- for (CellRangeAddress cellAddresses : cellList) {
|
|
|
|
|
- final int firstRow = cellAddresses.getFirstRow();
|
|
|
|
|
- if (cellAddresses.isInRange(cell) && rowIndex != firstRow) {
|
|
|
|
|
- cell.setBlank();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ for (CellRangeAddress cellAddresses : cellList) {
|
|
|
|
|
+ final int firstRow = cellAddresses.getFirstRow();
|
|
|
|
|
+ if (cellAddresses.isInRange(cell) && rowIndex != firstRow) {
|
|
|
|
|
+ cell.setBlank();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void afterWorkbookDispose(final WorkbookWriteHandlerContext context) {
|
|
public void afterWorkbookDispose(final WorkbookWriteHandlerContext context) {
|
|
|
- //当前表格写完后,统一写入
|
|
|
|
|
- if (CollUtil.isNotEmpty(cellList)) {
|
|
|
|
|
- for (CellRangeAddress item : cellList) {
|
|
|
|
|
- context.getWriteContext().writeSheetHolder().getSheet().addMergedRegion(item);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (CollUtil.isEmpty(cellList)) {
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @SneakyThrows
|
|
|
|
|
- private List<CellRangeAddress> handle(List<?> list, boolean hasTitle) {
|
|
|
|
|
- List<CellRangeAddress> cellList = new ArrayList<>();
|
|
|
|
|
- if (CollUtil.isEmpty(list)) {
|
|
|
|
|
- return cellList;
|
|
|
|
|
- }
|
|
|
|
|
- Class<?> clazz = list.get(0).getClass();
|
|
|
|
|
- boolean annotationPresent = clazz.isAnnotationPresent(ExcelIgnoreUnannotated.class);
|
|
|
|
|
- Field[] fields = ReflectUtils.getFields(clazz, field -> {
|
|
|
|
|
- if ("serialVersionUID".equals(field.getName())) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
- if (field.isAnnotationPresent(ExcelIgnore.class)) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
- return !annotationPresent || field.isAnnotationPresent(ExcelProperty.class);
|
|
|
|
|
- });
|
|
|
|
|
- // 有注解的字段
|
|
|
|
|
- List<Field> mergeFields = new ArrayList<>();
|
|
|
|
|
- List<Integer> mergeFieldsIndex = new ArrayList<>();
|
|
|
|
|
- for (int i = 0; i < fields.length; i++) {
|
|
|
|
|
- Field field = fields[i];
|
|
|
|
|
- if (field.isAnnotationPresent(CellMerge.class)) {
|
|
|
|
|
- CellMerge cm = field.getAnnotation(CellMerge.class);
|
|
|
|
|
- mergeFields.add(field);
|
|
|
|
|
- mergeFieldsIndex.add(cm.index() == -1 ? i : cm.index());
|
|
|
|
|
- if (hasTitle) {
|
|
|
|
|
- ExcelProperty property = field.getAnnotation(ExcelProperty.class);
|
|
|
|
|
- rowIndex = Math.max(rowIndex, property.value().length);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- Map<Field, RepeatCell> map = new HashMap<>();
|
|
|
|
|
- // 生成两两合并单元格
|
|
|
|
|
- for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
- Object rowObj = list.get(i);
|
|
|
|
|
- for (int j = 0; j < mergeFields.size(); j++) {
|
|
|
|
|
- Field field = mergeFields.get(j);
|
|
|
|
|
- Object val = ReflectUtils.invokeGetter(rowObj, field.getName());
|
|
|
|
|
-
|
|
|
|
|
- int colNum = mergeFieldsIndex.get(j);
|
|
|
|
|
- if (!map.containsKey(field)) {
|
|
|
|
|
- map.put(field, new RepeatCell(val, i));
|
|
|
|
|
- } else {
|
|
|
|
|
- RepeatCell repeatCell = map.get(field);
|
|
|
|
|
- Object cellValue = repeatCell.getValue();
|
|
|
|
|
- if (cellValue == null || "".equals(cellValue)) {
|
|
|
|
|
- // 空值跳过不合并
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (!cellValue.equals(val)) {
|
|
|
|
|
- if ((i - repeatCell.getCurrent() > 1)) {
|
|
|
|
|
- cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum));
|
|
|
|
|
- }
|
|
|
|
|
- map.put(field, new RepeatCell(val, i));
|
|
|
|
|
- } else if (i == list.size() - 1) {
|
|
|
|
|
- if (!isMerge(list, i, field)) {
|
|
|
|
|
- // 如果最后一行不能合并,检查之前的数据是否需要合并
|
|
|
|
|
- if (i - repeatCell.getCurrent() > 1) {
|
|
|
|
|
- cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum));
|
|
|
|
|
- }
|
|
|
|
|
- } else if (i > repeatCell.getCurrent()) {
|
|
|
|
|
- // 如果最后一行可以合并,则直接合并到最后
|
|
|
|
|
- cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
|
|
|
|
|
- }
|
|
|
|
|
- } else if (!isMerge(list, i, field)) {
|
|
|
|
|
- if ((i - repeatCell.getCurrent() > 1)) {
|
|
|
|
|
- cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum));
|
|
|
|
|
- }
|
|
|
|
|
- map.put(field, new RepeatCell(val, i));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return cellList;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private boolean isMerge(List<?> list, int i, Field field) {
|
|
|
|
|
- boolean isMerge = true;
|
|
|
|
|
- CellMerge cm = field.getAnnotation(CellMerge.class);
|
|
|
|
|
- final String[] mergeBy = cm.mergeBy();
|
|
|
|
|
- if (StrUtil.isAllNotBlank(mergeBy)) {
|
|
|
|
|
- //比对当前list(i)和list(i - 1)的各个属性值一一比对 如果全为真 则为真
|
|
|
|
|
- for (String fieldName : mergeBy) {
|
|
|
|
|
- final Object valCurrent = ReflectUtil.getFieldValue(list.get(i), fieldName);
|
|
|
|
|
- final Object valPre = ReflectUtil.getFieldValue(list.get(i - 1), fieldName);
|
|
|
|
|
- if (!Objects.equals(valPre, valCurrent)) {
|
|
|
|
|
- //依赖字段如有任一不等值,则标记为不可合并
|
|
|
|
|
- isMerge = false;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //当前表格写完后,统一写入
|
|
|
|
|
+ for (CellRangeAddress item : cellList) {
|
|
|
|
|
+ context.getWriteContext().writeSheetHolder().getSheet().addMergedRegion(item);
|
|
|
}
|
|
}
|
|
|
- return isMerge;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Data
|
|
|
|
|
- @AllArgsConstructor
|
|
|
|
|
- static class RepeatCell {
|
|
|
|
|
-
|
|
|
|
|
- private Object value;
|
|
|
|
|
-
|
|
|
|
|
- private int current;
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|