Browse Source

Make columns sort by parameter order and modified some typos

developing
dota17 5 years ago
parent
commit
8074ac28f9
  1. 97
      src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
  2. 4
      src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java
  3. 20
      src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java
  4. 40
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

97
src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java

@ -2,6 +2,7 @@ package com.alibaba.excel.metadata.property;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -24,6 +25,8 @@ import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder;
import javafx.util.Pair;
/**
* Define the header attribute of excel
*
@ -61,7 +64,7 @@ public class ExcelHeadProperty {
*/
private Map<String, Field> ignoreMap;
public ExcelHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
public ExcelHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllField) {
this.headClazz = headClazz;
headMap = new TreeMap<Integer, Head>();
contentPropertyMap = new TreeMap<Integer, ExcelContentProperty>();
@ -84,7 +87,7 @@ public class ExcelHeadProperty {
headKind = HeadKindEnum.STRING;
}
// convert headClazz to head
initColumnProperties(holder, convertAllFiled);
initColumnProperties(holder, convertAllField);
initHeadRowNumber();
if (LOGGER.isDebugEnabled()) {
@ -112,32 +115,98 @@ public class ExcelHeadProperty {
}
}
private void initColumnProperties(Holder holder, Boolean convertAllFiled) {
private void initColumnProperties(Holder holder, Boolean convertAllField) {
if (headClazz == null) {
return;
}
// Declared fields
List<Field> defaultFieldList = new ArrayList<Field>();
Map<Integer, Field> customFiledMap = new TreeMap<Integer, Field>();
ClassUtils.declaredFields(headClazz, defaultFieldList, customFiledMap, ignoreMap, convertAllFiled);
Map<Integer, Field> customFieldMap = new TreeMap<Integer, Field>();
ClassUtils.declaredFields(headClazz, defaultFieldList, customFieldMap, ignoreMap, convertAllField);
List<Pair<Field, Boolean>> exportFieldBoolPairsList = new ArrayList<Pair<Field, Boolean>>();
int index = 0;
for (Field field : defaultFieldList) {
while (customFiledMap.containsKey(index)) {
Field customFiled = customFiledMap.get(index);
customFiledMap.remove(index);
if (!initOneColumnProperty(holder, index, customFiled, Boolean.TRUE)) {
while (customFieldMap.containsKey(index)) {
Field field = customFieldMap.get(index);
Pair<Field, Boolean> fieldBooleanPair = new Pair<Field, Boolean>(field, Boolean.TRUE);
exportFieldBoolPairsList.add(fieldBooleanPair);
index++;
}
for (Field field : defaultFieldList) {
Pair<Field, Boolean> fieldBoolPair = new Pair<Field, Boolean>(field, Boolean.FALSE);
exportFieldBoolPairsList.add(fieldBoolPair);
}
sortExportColumnFields(holder, exportFieldBoolPairsList);
initColumnProperties(holder, exportFieldBoolPairsList);
for (Map.Entry<Integer, Field> entry : customFieldMap.entrySet()) {
initOneColumnProperty(holder, entry.getKey(), entry.getValue(), Boolean.TRUE);
}
headKind = HeadKindEnum.CLASS;
}
/**
* Give the field and flag pair list and arrange them in the specified order according to the user's settings.
* The field is what the user want to export to excel, the flag indicates whether the order of the field in excel is
* specified.
*
* @param holder Write holder which keeps the parameters of a sheet.
* @param exportFieldBoolPairList Keep all the fields and the flag(which indicate whether the field order is specified)
* of the head class except the ignored. It will be modified after this function is called.
*/
private void sortExportColumnFields(Holder holder, List<Pair<Field, Boolean>> exportFieldBoolPairList) {
if (holder instanceof AbstractWriteHolder) {
Collection<String> includeColumnFieldNames = ((AbstractWriteHolder) holder).getIncludeColumnFieldNames();
if (includeColumnFieldNames != null) {
Map<String, Pair<Field, Boolean>> exportFieldMap = new TreeMap<String, Pair<Field, Boolean>>();
List<String> includeColumnFieldNameList = new ArrayList<String>(includeColumnFieldNames);
for (Pair<Field, Boolean> fieldBoolPair : exportFieldBoolPairList) {
if (includeColumnFieldNameList.contains(fieldBoolPair.getKey().getName())) {
exportFieldMap.put(fieldBoolPair.getKey().getName(), fieldBoolPair);
}
}
exportFieldBoolPairList.clear();
for (String fieldName : includeColumnFieldNameList) {
exportFieldBoolPairList.add(exportFieldMap.get(fieldName));
}
return;
}
Collection<Integer> includeColumnIndexes = ((AbstractWriteHolder) holder).getIncludeColumnIndexes();
if (includeColumnIndexes != null) {
List<Pair<Field, Boolean>> tempFieldsList = new ArrayList<Pair<Field, Boolean>>();
for (Integer includeColumnIndex : includeColumnIndexes) {
tempFieldsList.add(exportFieldBoolPairList.get(includeColumnIndex));
}
exportFieldBoolPairList.clear();
exportFieldBoolPairList.addAll(tempFieldsList);
return;
}
int index = 0;
for (Pair<Field, Boolean> fieldBoolPair : exportFieldBoolPairList) {
if (((AbstractWriteHolder) holder).ignore(fieldBoolPair.getKey().getName(), index)) {
exportFieldBoolPairList.remove(fieldBoolPair);
}
if (!initOneColumnProperty(holder, index, field, Boolean.FALSE)) {
index++;
}
}
for (Map.Entry<Integer, Field> entry : customFiledMap.entrySet()) {
initOneColumnProperty(holder, entry.getKey(), entry.getValue(), Boolean.TRUE);
}
headKind = HeadKindEnum.CLASS;
/**
* Initialize column properties.
*
* @param holder Write holder which keeps the parameters of a sheet.
* @param exportFieldBoolPairList Keep the fields which will be exported to excel and the flag which indicate whether
* the field order in excel is specified.
*/
private void initColumnProperties(Holder holder, List<Pair<Field, Boolean>> exportFieldBoolPairList) {
int index = 0;
for (Pair<Field, Boolean> fieldBoolPair : exportFieldBoolPairList) {
initOneColumnProperty(holder, index, fieldBoolPair.getKey(), fieldBoolPair.getValue());
index++;
}
}
/**

4
src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java

@ -81,7 +81,7 @@ public abstract class AbstractExcelWriterParameterBuilder<T extends AbstractExce
* Ignore the custom columns.
*/
public T excludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
parameter().setExcludeColumnFiledNames(excludeColumnFiledNames);
parameter().setExcludeColumnFieldNames(excludeColumnFiledNames);
return self();
}
@ -97,7 +97,7 @@ public abstract class AbstractExcelWriterParameterBuilder<T extends AbstractExce
* Only output the custom columns.
*/
public T includeColumnFiledNames(Collection<String> includeColumnFiledNames) {
parameter().setIncludeColumnFiledNames(includeColumnFiledNames);
parameter().setIncludeColumnFieldNames(includeColumnFiledNames);
return self();
}

20
src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java

@ -40,7 +40,7 @@ public class WriteBasicParameter extends BasicParameter {
/**
* Ignore the custom columns.
*/
private Collection<String> excludeColumnFiledNames;
private Collection<String> excludeColumnFieldNames;
/**
* Only output the custom columns.
*/
@ -48,7 +48,7 @@ public class WriteBasicParameter extends BasicParameter {
/**
* Only output the custom columns.
*/
private Collection<String> includeColumnFiledNames;
private Collection<String> includeColumnFieldNames;
public Integer getRelativeHeadRowIndex() {
return relativeHeadRowIndex;
@ -98,12 +98,12 @@ public class WriteBasicParameter extends BasicParameter {
this.excludeColumnIndexes = excludeColumnIndexes;
}
public Collection<String> getExcludeColumnFiledNames() {
return excludeColumnFiledNames;
public Collection<String> getExcludeColumnFieldNames() {
return excludeColumnFieldNames;
}
public void setExcludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
this.excludeColumnFiledNames = excludeColumnFiledNames;
public void setExcludeColumnFieldNames(Collection<String> excludeColumnFieldNames) {
this.excludeColumnFieldNames = excludeColumnFieldNames;
}
public Collection<Integer> getIncludeColumnIndexes() {
@ -114,12 +114,12 @@ public class WriteBasicParameter extends BasicParameter {
this.includeColumnIndexes = includeColumnIndexes;
}
public Collection<String> getIncludeColumnFiledNames() {
return includeColumnFiledNames;
public Collection<String> getIncludeColumnFieldNames() {
return includeColumnFieldNames;
}
public void setIncludeColumnFiledNames(Collection<String> includeColumnFiledNames) {
this.includeColumnFiledNames = includeColumnFiledNames;
public void setIncludeColumnFieldNames(Collection<String> includeColumnFieldNames) {
this.includeColumnFieldNames = includeColumnFieldNames;
}
}

40
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -82,7 +82,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
/**
* Ignore the custom columns.
*/
private Collection<String> excludeColumnFiledNames;
private Collection<String> excludeColumnFieldNames;
/**
* Only output the custom columns.
*/
@ -90,10 +90,10 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
/**
* Only output the custom columns.
*/
private Collection<String> includeColumnFiledNames;
private Collection<String> includeColumnFieldNames;
public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder,
Boolean convertAllFiled) {
Boolean convertAllField) {
super(writeBasicParameter, parentAbstractWriteHolder);
if (writeBasicParameter.getUse1904windowing() == null) {
if (parentAbstractWriteHolder == null) {
@ -146,20 +146,20 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
this.automaticMergeHead = writeBasicParameter.getAutomaticMergeHead();
}
if (writeBasicParameter.getExcludeColumnFiledNames() == null && parentAbstractWriteHolder != null) {
this.excludeColumnFiledNames = parentAbstractWriteHolder.getExcludeColumnFiledNames();
if (writeBasicParameter.getExcludeColumnFieldNames() == null && parentAbstractWriteHolder != null) {
this.excludeColumnFieldNames = parentAbstractWriteHolder.getExcludeColumnFieldNames();
} else {
this.excludeColumnFiledNames = writeBasicParameter.getExcludeColumnFiledNames();
this.excludeColumnFieldNames = writeBasicParameter.getExcludeColumnFieldNames();
}
if (writeBasicParameter.getExcludeColumnIndexes() == null && parentAbstractWriteHolder != null) {
this.excludeColumnIndexes = parentAbstractWriteHolder.getExcludeColumnIndexes();
} else {
this.excludeColumnIndexes = writeBasicParameter.getExcludeColumnIndexes();
}
if (writeBasicParameter.getIncludeColumnFiledNames() == null && parentAbstractWriteHolder != null) {
this.includeColumnFiledNames = parentAbstractWriteHolder.getIncludeColumnFiledNames();
if (writeBasicParameter.getIncludeColumnFieldNames() == null && parentAbstractWriteHolder != null) {
this.includeColumnFieldNames = parentAbstractWriteHolder.getIncludeColumnFieldNames();
} else {
this.includeColumnFiledNames = writeBasicParameter.getIncludeColumnFiledNames();
this.includeColumnFieldNames = writeBasicParameter.getIncludeColumnFieldNames();
}
if (writeBasicParameter.getIncludeColumnIndexes() == null && parentAbstractWriteHolder != null) {
this.includeColumnIndexes = parentAbstractWriteHolder.getIncludeColumnIndexes();
@ -168,7 +168,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
}
// Initialization property
this.excelWriteHeadProperty = new ExcelWriteHeadProperty(this, getClazz(), getHead(), convertAllFiled);
this.excelWriteHeadProperty = new ExcelWriteHeadProperty(this, getClazz(), getHead(), convertAllField);
// Compatible with old code
compatibleOldCode(writeBasicParameter);
@ -446,10 +446,10 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
@Override
public boolean ignore(String fieldName, Integer columnIndex) {
if (fieldName != null) {
if (includeColumnFiledNames != null && !includeColumnFiledNames.contains(fieldName)) {
if (includeColumnFieldNames != null && !includeColumnFieldNames.contains(fieldName)) {
return true;
}
if (excludeColumnFiledNames != null && excludeColumnFiledNames.contains(fieldName)) {
if (excludeColumnFieldNames != null && excludeColumnFieldNames.contains(fieldName)) {
return true;
}
}
@ -520,12 +520,12 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
this.excludeColumnIndexes = excludeColumnIndexes;
}
public Collection<String> getExcludeColumnFiledNames() {
return excludeColumnFiledNames;
public Collection<String> getExcludeColumnFieldNames() {
return excludeColumnFieldNames;
}
public void setExcludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
this.excludeColumnFiledNames = excludeColumnFiledNames;
public void setExcludeColumnFieldNames(Collection<String> excludeColumnFieldNames) {
this.excludeColumnFieldNames = excludeColumnFieldNames;
}
public Collection<Integer> getIncludeColumnIndexes() {
@ -536,12 +536,12 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
this.includeColumnIndexes = includeColumnIndexes;
}
public Collection<String> getIncludeColumnFiledNames() {
return includeColumnFiledNames;
public Collection<String> getIncludeColumnFieldNames() {
return includeColumnFieldNames;
}
public void setIncludeColumnFiledNames(Collection<String> includeColumnFiledNames) {
this.includeColumnFiledNames = includeColumnFiledNames;
public void setIncludeColumnFieldNames(Collection<String> includeColumnFieldNames) {
this.includeColumnFieldNames = includeColumnFieldNames;
}
@Override

Loading…
Cancel
Save