From 0a34afdc8d0b5c03e425de4722af107716ce11b1 Mon Sep 17 00:00:00 2001 From: hccake Date: Fri, 9 Apr 2021 17:52:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E5=BD=93?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E8=A2=AB=E5=BF=BD=E7=95=A5=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E4=BC=9A=E5=AF=BC=E8=87=B4=E5=90=8E=E7=BB=AD=E5=88=97=E5=90=8E?= =?UTF-8?q?=E7=A7=BB=EF=BC=8Cexcel=E5=A4=9A=E4=BA=86=E7=A9=BA=E7=99=BD?= =?UTF-8?q?=E5=88=97=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/excel/util/ClassUtils.java | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/alibaba/excel/util/ClassUtils.java b/src/main/java/com/alibaba/excel/util/ClassUtils.java index 936c325d..5f753131 100644 --- a/src/main/java/com/alibaba/excel/util/ClassUtils.java +++ b/src/main/java/com/alibaba/excel/util/ClassUtils.java @@ -1,5 +1,13 @@ 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.reflect.Field; import java.lang.reflect.Modifier; @@ -11,16 +19,6 @@ import java.util.Map; import java.util.TreeMap; 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 * @@ -47,25 +45,32 @@ public class ClassUtils { } tempIndexFildMap.putAll(fieldCache.getIndexFiledMap()); + Map originSortedAllFiledMap = fieldCache.getSortedAllFiledMap(); if (!needIgnore) { - sortedAllFiledMap.putAll(fieldCache.getSortedAllFiledMap()); + sortedAllFiledMap.putAll(originSortedAllFiledMap); return; } - int index = 0; - for (Map.Entry entry : fieldCache.getSortedAllFiledMap().entrySet()) { - Field field = entry.getValue(); - if (((WriteHolder) holder).ignore(entry.getValue().getName(), entry.getKey())) { - if (ignoreMap != null) { - ignoreMap.put(field.getName(), field); - } - while (tempIndexFildMap.containsKey(index)) { - tempIndexFildMap.remove(index); - index++; + // 获取到属性字段的最大index + int maxIndex = -1; + for (Integer filedIndex : originSortedAllFiledMap.keySet()) { + maxIndex = Math.max(filedIndex, maxIndex); + } + // 被忽略的属性数量 + int ignoreNum = 0; + // 当有属性被忽略时,需要将其后面的所有属性 index 前移 + for (int index = 0; index <= maxIndex; 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 { - sortedAllFiledMap.put(index, field); - index++; + tempIndexFildMap.remove(index); + ignoreNum++; + } else if(field != null){ + int finalIndex = index - ignoreNum; + sortedAllFiledMap.put(finalIndex, field); } } } From 52df21d5545cac282cccdd9dd8efc3f67091445a Mon Sep 17 00:00:00 2001 From: hccake Date: Wed, 28 Apr 2021 22:31:50 +0800 Subject: [PATCH 2/2] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E5=BD=93?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=20header=20=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=9C=A8=20writeSheet=20=E5=AF=B9=E8=B1=A1=E4=B8=8A=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E5=AD=97=E6=AE=B5=E5=90=8D=E5=BF=BD=E7=95=A5=E4=B8=8D?= =?UTF-8?q?=E7=94=9F=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../write/executor/ExcelWriteAddExecutor.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java index 9f229029..bd655776 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -1,16 +1,5 @@ package com.alibaba.excel.write.executor; -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; - -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.Row; - import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.CellData; @@ -20,13 +9,20 @@ import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; -import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder; import com.alibaba.excel.write.metadata.holder.WriteHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; -import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; - import net.sf.cglib.beans.BeanMap; +import org.apache.poi.ss.usermodel.Cell; +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 @@ -171,14 +167,17 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { if (!sortedAllFiledMap.isEmpty()) { return; } - WriteWorkbookHolder writeWorkbookHolder = writeContext.writeWorkbookHolder(); - boolean needIgnore = - !CollectionUtils.isEmpty(writeWorkbookHolder.getExcludeColumnFiledNames()) || !CollectionUtils - .isEmpty(writeWorkbookHolder.getExcludeColumnIndexes()) || !CollectionUtils - .isEmpty(writeWorkbookHolder.getIncludeColumnFiledNames()) || !CollectionUtils - .isEmpty(writeWorkbookHolder.getIncludeColumnIndexes()); + + // 获取当前的使用的 holder + WriteHolder holder = writeContext.currentWriteHolder(); + boolean needIgnore = (holder instanceof AbstractWriteHolder) && ( + !CollectionUtils.isEmpty(((AbstractWriteHolder) holder).getExcludeColumnFiledNames()) || !CollectionUtils + .isEmpty(((AbstractWriteHolder) holder).getExcludeColumnIndexes()) || !CollectionUtils + .isEmpty(((AbstractWriteHolder) holder).getIncludeColumnFiledNames()) || !CollectionUtils + .isEmpty(((AbstractWriteHolder) holder).getIncludeColumnIndexes())); + ClassUtils.declaredFields(clazz, sortedAllFiledMap, - writeWorkbookHolder.getWriteWorkbook().getConvertAllFiled(), needIgnore, writeWorkbookHolder); + writeContext.writeWorkbookHolder().getWriteWorkbook().getConvertAllFiled(), needIgnore, holder); } }