!481 单元格合并可以基于注解选择哪些其他字段需要同时相等方可进行该单元格合并
* excel插件 原有的合并策略写死了根据前一字段是否一致来判断后一字段的合并 感觉这样不灵活 如有特殊需求 字段排序不规整 于是扩充了注解的…
This commit is contained in:
parent
7b4e8324a9
commit
70bf1a48d0
@ -21,4 +21,9 @@ public @interface CellMerge {
|
|||||||
*/
|
*/
|
||||||
int index() default -1;
|
int index() default -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 合并需要依赖的其他字段名称
|
||||||
|
*/
|
||||||
|
String[] mergeBy() default {};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package org.dromara.common.excel.core;
|
package org.dromara.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 com.alibaba.excel.annotation.ExcelProperty;
|
import com.alibaba.excel.annotation.ExcelProperty;
|
||||||
import com.alibaba.excel.metadata.Head;
|
import com.alibaba.excel.metadata.Head;
|
||||||
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
|
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
|
||||||
@ -15,10 +17,7 @@ import org.dromara.common.core.utils.reflect.ReflectUtils;
|
|||||||
import org.dromara.common.excel.annotation.CellMerge;
|
import org.dromara.common.excel.annotation.CellMerge;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 列值重复合并策略
|
* 列值重复合并策略
|
||||||
@ -93,43 +92,41 @@ public class CellMergeStrategy extends AbstractMergeStrategy {
|
|||||||
// 空值跳过不合并
|
// 空值跳过不合并
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cellValue.equals(val)) {
|
if (!cellValue.equals(val)) {
|
||||||
if (i - repeatCell.getCurrent() > 1) {
|
if ((i - repeatCell.getCurrent() > 1) && isMerge(list, i, field)) {
|
||||||
cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum));
|
cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum));
|
||||||
}
|
}
|
||||||
map.put(field, new RepeatCell(val, i));
|
map.put(field, new RepeatCell(val, i));
|
||||||
} else if (j == 0) {
|
|
||||||
if (i == list.size() - 1) {
|
|
||||||
if (i > repeatCell.getCurrent()) {
|
|
||||||
cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 判断前面的是否合并了
|
|
||||||
RepeatCell firstCell = map.get(mergeFields.get(0));
|
|
||||||
if (repeatCell.getCurrent() != firstCell.getCurrent()) {
|
|
||||||
if (i == list.size() - 1) {
|
|
||||||
if (i > repeatCell.getCurrent()) {
|
|
||||||
cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
|
|
||||||
}
|
|
||||||
} else if (repeatCell.getCurrent() < firstCell.getCurrent()) {
|
|
||||||
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) {
|
} else if (i == list.size() - 1) {
|
||||||
if (i > repeatCell.getCurrent()) {
|
if (i > repeatCell.getCurrent() && isMerge(list, i, field)) {
|
||||||
cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
|
cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return cellList;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isMerge;
|
||||||
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
static class RepeatCell {
|
static class RepeatCell {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user