Browse Source

🐛 修复当属性被忽略时,会导致后续列后移,excel多了空白列的问题

pull/1718/head
hccake 4 years ago
parent
commit
0a34afdc8d
  1. 53
      src/main/java/com/alibaba/excel/util/ClassUtils.java

53
src/main/java/com/alibaba/excel/util/ClassUtils.java

@ -1,5 +1,13 @@
package com.alibaba.excel.util; package com.alibaba.excel.util;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.write.metadata.holder.WriteHolder;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -11,16 +19,6 @@ import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.event.Handler;
import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteHolder;
/** /**
* Class utils * Class utils
* *
@ -47,25 +45,32 @@ public class ClassUtils {
} }
tempIndexFildMap.putAll(fieldCache.getIndexFiledMap()); tempIndexFildMap.putAll(fieldCache.getIndexFiledMap());
Map<Integer, Field> originSortedAllFiledMap = fieldCache.getSortedAllFiledMap();
if (!needIgnore) { if (!needIgnore) {
sortedAllFiledMap.putAll(fieldCache.getSortedAllFiledMap()); sortedAllFiledMap.putAll(originSortedAllFiledMap);
return; return;
} }
int index = 0; // 获取到属性字段的最大index
for (Map.Entry<Integer, Field> entry : fieldCache.getSortedAllFiledMap().entrySet()) { int maxIndex = -1;
Field field = entry.getValue(); for (Integer filedIndex : originSortedAllFiledMap.keySet()) {
if (((WriteHolder) holder).ignore(entry.getValue().getName(), entry.getKey())) { maxIndex = Math.max(filedIndex, maxIndex);
if (ignoreMap != null) { }
ignoreMap.put(field.getName(), field); // 被忽略的属性数量
} int ignoreNum = 0;
while (tempIndexFildMap.containsKey(index)) { // 当有属性被忽略时,需要将其后面的所有属性 index 前移
tempIndexFildMap.remove(index); for (int index = 0; index <= maxIndex; index++) {
index++; Field field = originSortedAllFiledMap.get(index);
String name = field == null? null: field.getName();
if (((WriteHolder) holder).ignore(name, index)) {
if (ignoreMap != null && name != null) {
ignoreMap.put(name, field);
} }
} else { tempIndexFildMap.remove(index);
sortedAllFiledMap.put(index, field); ignoreNum++;
index++; } else if(field != null){
int finalIndex = index - ignoreNum;
sortedAllFiledMap.put(finalIndex, field);
} }
} }
} }

Loading…
Cancel
Save