Browse Source

Merge pull request #1718 from Hccake/master

修复当自定义 header 时,在 writeSheet 对象上指定字段名忽略不生效的问题 & 修复当属性被忽略时,会导致后续列后移,excel多了空白列的问题
developing
Jiaju Zhuang 3 years ago committed by GitHub
parent
commit
9603d470c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      src/main/java/com/alibaba/excel/util/ClassUtils.java
  2. 36
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

44
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;
@ -13,6 +21,7 @@ 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.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
@ -44,25 +53,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);
} }
} }
} }

36
src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -1,5 +1,6 @@
package com.alibaba.excel.write.executor; package com.alibaba.excel.write.executor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -18,18 +19,23 @@ import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.FieldUtils;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils; import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.metadata.CollectionRowData; import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder;
import com.alibaba.excel.write.metadata.MapRowData;
import com.alibaba.excel.write.metadata.RowData;
import com.alibaba.excel.write.metadata.holder.WriteHolder; import com.alibaba.excel.write.metadata.holder.WriteHolder;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import net.sf.cglib.beans.BeanMap; import net.sf.cglib.beans.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/** /**
* Add the data into excel * Add the data into excel
* *
@ -183,13 +189,17 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
if (!sortedAllFiledMap.isEmpty()) { if (!sortedAllFiledMap.isEmpty()) {
return; return;
} }
WriteWorkbookHolder writeWorkbookHolder = writeContext.writeWorkbookHolder();
boolean needIgnore = // 获取当前的使用的 holder
!CollectionUtils.isEmpty(writeWorkbookHolder.getExcludeColumnFieldNames()) || !CollectionUtils WriteHolder holder = writeContext.currentWriteHolder();
.isEmpty(writeWorkbookHolder.getExcludeColumnIndexes()) || !CollectionUtils boolean needIgnore = (holder instanceof AbstractWriteHolder) && (
.isEmpty(writeWorkbookHolder.getIncludeColumnFieldNames()) || !CollectionUtils !CollectionUtils.isEmpty(((AbstractWriteHolder) holder).getExcludeColumnFiledNames()) || !CollectionUtils
.isEmpty(writeWorkbookHolder.getIncludeColumnIndexes()); .isEmpty(((AbstractWriteHolder) holder).getExcludeColumnIndexes()) || !CollectionUtils
ClassUtils.declaredFields(clazz, sortedAllFiledMap, needIgnore, writeWorkbookHolder); .isEmpty(((AbstractWriteHolder) holder).getIncludeColumnFiledNames()) || !CollectionUtils
.isEmpty(((AbstractWriteHolder) holder).getIncludeColumnIndexes()));
ClassUtils.declaredFields(clazz, sortedAllFiledMap,
writeContext.writeWorkbookHolder().getWriteWorkbook().getConvertAllFiled(), needIgnore, holder);
} }
} }

Loading…
Cancel
Save