Browse Source

support force index

pull/3156/head
gongxuanzhang 2 years ago
parent
commit
9ad63b696e
  1. 34
      easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java
  2. 9
      easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java
  3. 4
      easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java
  4. 10
      easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  5. 35
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java

34
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.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; 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.FontProperty;
import com.alibaba.excel.metadata.property.NumberFormatProperty; import com.alibaba.excel.metadata.property.NumberFormatProperty;
import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.metadata.property.StyleProperty;
import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder;
import com.alibaba.excel.write.metadata.holder.WriteHolder; import com.alibaba.excel.write.metadata.holder.WriteHolder;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -36,6 +39,7 @@ import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.springframework.cglib.beans.BeanMap; import org.springframework.cglib.beans.BeanMap;
import org.springframework.util.CollectionUtils;
/** /**
* Licensed to the Apache Software Foundation (ASF) under one or more * 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<Integer, Field> sortedAllFieldMap) {
if (!(holder instanceof AbstractWriteHolder)) {
return;
}
AbstractWriteHolder writeHolder = (AbstractWriteHolder)holder;
Collection<String> allCol = writeHolder.getIncludeColumnFieldNames();
if (!CollectionUtils.isEmpty(allCol) && writeHolder.getForceIndex() != null && writeHolder.getForceIndex()) {
Map<String, Integer> colIndexMap = MapUtils.newHashMap();
Iterator<String> iterator = allCol.iterator();
int colIndex = 0;
while (iterator.hasNext()) {
String col = iterator.next();
colIndexMap.put(col, colIndex);
colIndex++;
}
Map<Integer, Field> 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<Integer, Field> sortedAllFieldMap, Boolean needIgnore, public static void declaredFields(Class<?> clazz, Map<Integer, Field> sortedAllFieldMap, Boolean needIgnore,

9
easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java

@ -120,4 +120,13 @@ public abstract class AbstractExcelWriterParameterBuilder<T extends AbstractExce
parameter().setIncludeColumnFieldNames(includeColumnFieldNames); parameter().setIncludeColumnFieldNames(includeColumnFieldNames);
return self(); return self();
} }
/**
* head index use {@link this#includeColumnFiledNames} sort
* useless if not set {@link this#includeColumnFiledNames}
**/
public T forceIndex(Boolean force) {
parameter().setForceIndex(force);
return self();
}
} }

4
easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java

@ -56,4 +56,8 @@ public class WriteBasicParameter extends BasicParameter {
* Only output the custom columns. * Only output the custom columns.
*/ */
private Collection<String> includeColumnFieldNames; private Collection<String> includeColumnFieldNames;
/**
* head sorted use includeColumnFieldNames sort
*/
private Boolean forceIndex;
} }

10
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. * Only output the custom columns.
*/ */
private Collection<String> includeColumnFieldNames; private Collection<String> includeColumnFieldNames;
/**
* head sorted use {@link #includeColumnFieldNames} sort
*/
private Boolean forceIndex;
/** /**
* Write handler * Write handler
*/ */
@ -194,6 +197,11 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
} else { } else {
this.includeColumnFieldNames = writeBasicParameter.getIncludeColumnFieldNames(); 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) { if (writeBasicParameter.getIncludeColumnIndexes() == null && parentAbstractWriteHolder != null) {
this.includeColumnIndexes = parentAbstractWriteHolder.getIncludeColumnIndexes(); this.includeColumnIndexes = parentAbstractWriteHolder.getIncludeColumnIndexes();
} else { } else {

35
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 includeFieldName07;
private static File includeFieldName03; private static File includeFieldName03;
private static File includeFieldNameCsv; private static File includeFieldNameCsv;
private static File includeFieldNameForceIndex07;
private static File includeFieldNameForceIndex03;
private static File includeFieldNameForceIndexCsv;
@BeforeClass @BeforeClass
public static void init() { public static void init() {
@ -49,6 +52,9 @@ public class ExcludeOrIncludeDataTest {
includeFieldName07 = TestFileUtil.createNewFile("includeFieldName.xlsx"); includeFieldName07 = TestFileUtil.createNewFile("includeFieldName.xlsx");
includeFieldName03 = TestFileUtil.createNewFile("includeFieldName.xls"); includeFieldName03 = TestFileUtil.createNewFile("includeFieldName.xls");
includeFieldNameCsv = TestFileUtil.createNewFile("includeFieldName.csv"); includeFieldNameCsv = TestFileUtil.createNewFile("includeFieldName.csv");
includeFieldNameForceIndex07 = TestFileUtil.createNewFile("includeFieldNameForceIndex.xlsx");
includeFieldNameForceIndex03 = TestFileUtil.createNewFile("includeFieldNameForceIndex.xls");
includeFieldNameForceIndexCsv = TestFileUtil.createNewFile("includeFieldNameForceIndex.csv");
} }
@Test @Test
@ -112,6 +118,21 @@ public class ExcludeOrIncludeDataTest {
includeFieldName(includeFieldNameCsv); 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) { private void excludeIndex(File file) {
Set<Integer> excludeColumnIndexes = new HashSet<Integer>(); Set<Integer> excludeColumnIndexes = new HashSet<Integer>();
excludeColumnIndexes.add(0); excludeColumnIndexes.add(0);
@ -171,6 +192,20 @@ public class ExcludeOrIncludeDataTest {
Assert.assertEquals("column3", record.get(1)); Assert.assertEquals("column3", record.get(1));
} }
private void includeFieldNameForce(File file) {
List<String> includeColumnFieldNames = new ArrayList<>();
includeColumnFieldNames.add("column3");
includeColumnFieldNames.add("column2");
EasyExcel.write(file, ExcludeOrIncludeData.class).includeColumnFieldNames(includeColumnFieldNames)
.forceIndex(true).sheet().doWrite(data());
List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync();
Assert.assertEquals(1, dataMap.size());
Map<Integer, String> record = dataMap.get(0);
Assert.assertEquals(2, record.size());
Assert.assertEquals("column3", record.get(0));
Assert.assertEquals("column2", record.get(1));
}
private List<ExcludeOrIncludeData> data() { private List<ExcludeOrIncludeData> data() {
List<ExcludeOrIncludeData> list = new ArrayList<ExcludeOrIncludeData>(); List<ExcludeOrIncludeData> list = new ArrayList<ExcludeOrIncludeData>();
ExcludeOrIncludeData excludeOrIncludeData = new ExcludeOrIncludeData(); ExcludeOrIncludeData excludeOrIncludeData = new ExcludeOrIncludeData();

Loading…
Cancel
Save