From 9ad63b696ee4951ddb7707e3b2d33d6a5c64c73e Mon Sep 17 00:00:00 2001 From: gongxuanzhang Date: Fri, 28 Apr 2023 18:01:17 +0800 Subject: [PATCH] support force index --- .../com/alibaba/excel/util/ClassUtils.java | 34 ++++++++++++++++++ .../AbstractExcelWriterParameterBuilder.java | 9 +++++ .../write/metadata/WriteBasicParameter.java | 4 +++ .../metadata/holder/AbstractWriteHolder.java | 10 +++++- .../ExcludeOrIncludeDataTest.java | 35 +++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java index 6bed3052..b42df514 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java @@ -3,9 +3,11 @@ package com.alibaba.excel.util; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -29,6 +31,7 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.NumberFormatProperty; import com.alibaba.excel.metadata.property.StyleProperty; +import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder; import com.alibaba.excel.write.metadata.holder.WriteHolder; import lombok.AllArgsConstructor; @@ -36,6 +39,7 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import org.springframework.cglib.beans.BeanMap; +import org.springframework.util.CollectionUtils; /** * Licensed to the Apache Software Foundation (ASF) under one or more @@ -250,6 +254,36 @@ public class ClassUtils { } } } + forceIndexIfNecessary(holder, sortedAllFieldMap); + } + + /** + * it only works when {@link AbstractWriteHolder#getIncludeColumnFieldNames()} has value + * and {@link AbstractWriteHolder#getForceIndex()} is true + **/ + private static void forceIndexIfNecessary(Holder holder, Map sortedAllFieldMap) { + if (!(holder instanceof AbstractWriteHolder)) { + return; + } + AbstractWriteHolder writeHolder = (AbstractWriteHolder)holder; + Collection allCol = writeHolder.getIncludeColumnFieldNames(); + if (!CollectionUtils.isEmpty(allCol) && writeHolder.getForceIndex() != null && writeHolder.getForceIndex()) { + Map colIndexMap = MapUtils.newHashMap(); + Iterator iterator = allCol.iterator(); + int colIndex = 0; + while (iterator.hasNext()) { + String col = iterator.next(); + colIndexMap.put(col, colIndex); + colIndex++; + } + Map temp = MapUtils.newHashMap(); + sortedAllFieldMap.forEach((index, field) -> { + Integer fieldIndex = colIndexMap.get(field.getName()); + temp.put(fieldIndex, field); + }); + sortedAllFieldMap.clear(); + sortedAllFieldMap.putAll(temp); + } } public static void declaredFields(Class clazz, Map sortedAllFieldMap, Boolean needIgnore, diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java index f8844a52..89ff01a3 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java @@ -120,4 +120,13 @@ public abstract class AbstractExcelWriterParameterBuilder includeColumnFieldNames; + /** + * head sorted use includeColumnFieldNames sort + */ + private Boolean forceIndex; } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index b6907e42..4d7bc476 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java @@ -94,7 +94,10 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ * Only output the custom columns. */ private Collection includeColumnFieldNames; - + /** + * head sorted use {@link #includeColumnFieldNames} sort + */ + private Boolean forceIndex; /** * Write handler */ @@ -194,6 +197,11 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ } else { this.includeColumnFieldNames = writeBasicParameter.getIncludeColumnFieldNames(); } + if (writeBasicParameter.getForceIndex() == null && parentAbstractWriteHolder != null) { + this.forceIndex = parentAbstractWriteHolder.getForceIndex(); + } else { + this.forceIndex = writeBasicParameter.getForceIndex(); + } if (writeBasicParameter.getIncludeColumnIndexes() == null && parentAbstractWriteHolder != null) { this.includeColumnIndexes = parentAbstractWriteHolder.getIncludeColumnIndexes(); } else { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java index 9bf69821..6bd824db 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java @@ -34,6 +34,9 @@ public class ExcludeOrIncludeDataTest { private static File includeFieldName07; private static File includeFieldName03; private static File includeFieldNameCsv; + private static File includeFieldNameForceIndex07; + private static File includeFieldNameForceIndex03; + private static File includeFieldNameForceIndexCsv; @BeforeClass public static void init() { @@ -49,6 +52,9 @@ public class ExcludeOrIncludeDataTest { includeFieldName07 = TestFileUtil.createNewFile("includeFieldName.xlsx"); includeFieldName03 = TestFileUtil.createNewFile("includeFieldName.xls"); includeFieldNameCsv = TestFileUtil.createNewFile("includeFieldName.csv"); + includeFieldNameForceIndex07 = TestFileUtil.createNewFile("includeFieldNameForceIndex.xlsx"); + includeFieldNameForceIndex03 = TestFileUtil.createNewFile("includeFieldNameForceIndex.xls"); + includeFieldNameForceIndexCsv = TestFileUtil.createNewFile("includeFieldNameForceIndex.csv"); } @Test @@ -112,6 +118,21 @@ public class ExcludeOrIncludeDataTest { includeFieldName(includeFieldNameCsv); } + @Test + public void t41IncludeFieldName07() { + includeFieldNameForce(includeFieldNameForceIndex07); + } + + @Test + public void t42IncludeFieldName03() { + includeFieldNameForce(includeFieldNameForceIndex03); + } + + @Test + public void t43IncludeFieldNameCsv() { + includeFieldNameForce(includeFieldNameForceIndexCsv); + } + private void excludeIndex(File file) { Set excludeColumnIndexes = new HashSet(); excludeColumnIndexes.add(0); @@ -171,6 +192,20 @@ public class ExcludeOrIncludeDataTest { Assert.assertEquals("column3", record.get(1)); } + private void includeFieldNameForce(File file) { + List includeColumnFieldNames = new ArrayList<>(); + includeColumnFieldNames.add("column3"); + includeColumnFieldNames.add("column2"); + EasyExcel.write(file, ExcludeOrIncludeData.class).includeColumnFieldNames(includeColumnFieldNames) + .forceIndex(true).sheet().doWrite(data()); + List> dataMap = EasyExcel.read(file).sheet().doReadSync(); + Assert.assertEquals(1, dataMap.size()); + Map record = dataMap.get(0); + Assert.assertEquals(2, record.size()); + Assert.assertEquals("column3", record.get(0)); + Assert.assertEquals("column2", record.get(1)); + } + private List data() { List list = new ArrayList(); ExcludeOrIncludeData excludeOrIncludeData = new ExcludeOrIncludeData();