Browse Source

* 修复`filed`拼接错误 [Issue #2390](https://github.com/alibaba/easyexcel/issues/2390)

pull/2430/head
Jiaju Zhuang 3 years ago
parent
commit
3e0d4a625f
  1. 2
      easyexcel-core/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java
  2. 10
      easyexcel-core/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
  3. 94
      easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java
  4. 4
      easyexcel-core/src/main/java/com/alibaba/excel/util/FieldUtils.java
  5. 27
      easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java
  6. 34
      easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  7. 70
      easyexcel-core/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java
  8. 12
      easyexcel-core/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  9. 10
      easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java
  10. 1
      update.md

2
easyexcel-core/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java

@ -18,7 +18,7 @@ public class ExcelContentProperty {
public static final ExcelContentProperty EMPTY = new ExcelContentProperty(); public static final ExcelContentProperty EMPTY = new ExcelContentProperty();
/** /**
* Java filed * Java field
*/ */
private Field field; private Field field;
/** /**

10
easyexcel-core/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java

@ -109,8 +109,8 @@ public class ExcelHeadProperty {
return; return;
} }
// Declared fields // Declared fields
Map<Integer, Field> sortedAllFiledMap = MapUtils.newTreeMap(); Map<Integer, Field> sortedAllFieldMap = MapUtils.newTreeMap();
Map<Integer, Field> indexFiledMap = MapUtils.newTreeMap(); Map<Integer, Field> indexFieldMap = MapUtils.newTreeMap();
boolean needIgnore = (holder instanceof AbstractWriteHolder) && ( boolean needIgnore = (holder instanceof AbstractWriteHolder) && (
!CollectionUtils.isEmpty(((AbstractWriteHolder)holder).getExcludeColumnFieldNames()) || !CollectionUtils !CollectionUtils.isEmpty(((AbstractWriteHolder)holder).getExcludeColumnFieldNames()) || !CollectionUtils
@ -118,10 +118,10 @@ public class ExcelHeadProperty {
.isEmpty(((AbstractWriteHolder)holder).getIncludeColumnFieldNames()) || !CollectionUtils .isEmpty(((AbstractWriteHolder)holder).getIncludeColumnFieldNames()) || !CollectionUtils
.isEmpty(((AbstractWriteHolder)holder).getIncludeColumnIndexes())); .isEmpty(((AbstractWriteHolder)holder).getIncludeColumnIndexes()));
ClassUtils.declaredFields(headClazz, sortedAllFiledMap, indexFiledMap, ignoreMap, needIgnore, holder); ClassUtils.declaredFields(headClazz, sortedAllFieldMap, indexFieldMap, ignoreMap, needIgnore, holder);
for (Map.Entry<Integer, Field> entry : sortedAllFiledMap.entrySet()) { for (Map.Entry<Integer, Field> entry : sortedAllFieldMap.entrySet()) {
initOneColumnProperty(entry.getKey(), entry.getValue(), indexFiledMap.containsKey(entry.getKey())); initOneColumnProperty(entry.getKey(), entry.getValue(), indexFieldMap.containsKey(entry.getKey()));
} }
headKind = HeadKindEnum.CLASS; headKind = HeadKindEnum.CLASS;
} }

94
easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java

@ -195,17 +195,17 @@ public class ClassUtils {
} }
/** /**
* Parsing filed in the class * Parsing field in the class
* *
* @param clazz Need to parse the class * @param clazz Need to parse the class
* @param sortedAllFiledMap Complete the map of sorts * @param sortedAllFieldMap Complete the map of sorts
* @param indexFiledMap Use the index to sort fields * @param indexFieldMap Use the index to sort fields
* @param ignoreMap You want to ignore field map * @param ignoreMap You want to ignore field map
* @param needIgnore If you want to ignore fields need to ignore * @param needIgnore If you want to ignore fields need to ignore
* @param holder holder * @param holder holder
*/ */
public static void declaredFields(Class<?> clazz, Map<Integer, Field> sortedAllFiledMap, public static void declaredFields(Class<?> clazz, Map<Integer, Field> sortedAllFieldMap,
Map<Integer, Field> indexFiledMap, Map<String, Field> ignoreMap, Boolean needIgnore, Holder holder) { Map<Integer, Field> indexFieldMap, Map<String, Field> ignoreMap, Boolean needIgnore, Holder holder) {
FieldCache fieldCache = declaredFields(clazz); FieldCache fieldCache = declaredFields(clazz);
if (fieldCache == null) { if (fieldCache == null) {
return; return;
@ -213,20 +213,20 @@ public class ClassUtils {
if (ignoreMap != null) { if (ignoreMap != null) {
ignoreMap.putAll(fieldCache.getIgnoreMap()); ignoreMap.putAll(fieldCache.getIgnoreMap());
} }
Map<Integer, Field> tempIndexFieldMap = indexFiledMap; Map<Integer, Field> tempIndexFieldMap = indexFieldMap;
if (tempIndexFieldMap == null) { if (tempIndexFieldMap == null) {
tempIndexFieldMap = MapUtils.newTreeMap(); tempIndexFieldMap = MapUtils.newTreeMap();
} }
tempIndexFieldMap.putAll(fieldCache.getIndexFiledMap()); tempIndexFieldMap.putAll(fieldCache.getIndexFieldMap());
Map<Integer, Field> originSortedAllFiledMap = fieldCache.getSortedAllFiledMap(); Map<Integer, Field> originSortedAllFieldMap = fieldCache.getSortedAllFieldMap();
if (!needIgnore) { if (!needIgnore) {
sortedAllFiledMap.putAll(originSortedAllFiledMap); sortedAllFieldMap.putAll(originSortedAllFieldMap);
return; return;
} }
int index = 0; int index = 0;
for (Map.Entry<Integer, Field> entry : originSortedAllFiledMap.entrySet()) { for (Map.Entry<Integer, Field> entry : originSortedAllFieldMap.entrySet()) {
Integer key = entry.getKey(); Integer key = entry.getKey();
Field field = entry.getValue(); Field field = entry.getValue();
@ -239,22 +239,22 @@ public class ClassUtils {
} else { } else {
// Mandatory sorted fields // Mandatory sorted fields
if (tempIndexFieldMap.containsKey(key)) { if (tempIndexFieldMap.containsKey(key)) {
sortedAllFiledMap.put(key, field); sortedAllFieldMap.put(key, field);
} else { } else {
// Need to reorder automatically // Need to reorder automatically
// Check whether the current key is already in use // Check whether the current key is already in use
while (sortedAllFiledMap.containsKey(index)) { while (sortedAllFieldMap.containsKey(index)) {
index++; index++;
} }
sortedAllFiledMap.put(index++, field); sortedAllFieldMap.put(index++, field);
} }
} }
} }
} }
public static void declaredFields(Class<?> clazz, Map<Integer, Field> sortedAllFiledMap, Boolean needIgnore, public static void declaredFields(Class<?> clazz, Map<Integer, Field> sortedAllFieldMap, Boolean needIgnore,
WriteHolder writeHolder) { WriteHolder writeHolder) {
declaredFields(clazz, sortedAllFiledMap, null, null, needIgnore, writeHolder); declaredFields(clazz, sortedAllFieldMap, null, null, needIgnore, writeHolder);
} }
private static FieldCache declaredFields(Class<?> clazz) { private static FieldCache declaredFields(Class<?> clazz) {
@ -272,43 +272,43 @@ public class ClassUtils {
tempClass = tempClass.getSuperclass(); tempClass = tempClass.getSuperclass();
} }
// Screening of field // Screening of field
Map<Integer, List<Field>> orderFiledMap = new TreeMap<Integer, List<Field>>(); Map<Integer, List<Field>> orderFieldMap = new TreeMap<Integer, List<Field>>();
Map<Integer, Field> indexFiledMap = new TreeMap<Integer, Field>(); Map<Integer, Field> indexFieldMap = new TreeMap<Integer, Field>();
Map<String, Field> ignoreMap = new HashMap<String, Field>(16); Map<String, Field> ignoreMap = new HashMap<String, Field>(16);
ExcelIgnoreUnannotated excelIgnoreUnannotated = clazz.getAnnotation(ExcelIgnoreUnannotated.class); ExcelIgnoreUnannotated excelIgnoreUnannotated = clazz.getAnnotation(ExcelIgnoreUnannotated.class);
for (Field field : tempFieldList) { for (Field field : tempFieldList) {
declaredOneField(field, orderFiledMap, indexFiledMap, ignoreMap, excelIgnoreUnannotated); declaredOneField(field, orderFieldMap, indexFieldMap, ignoreMap, excelIgnoreUnannotated);
} }
return new FieldCache(buildSortedAllFiledMap(orderFiledMap, indexFiledMap), indexFiledMap, ignoreMap); return new FieldCache(buildSortedAllFieldMap(orderFieldMap, indexFieldMap), indexFieldMap, ignoreMap);
}); });
} }
private static Map<Integer, Field> buildSortedAllFiledMap(Map<Integer, List<Field>> orderFiledMap, private static Map<Integer, Field> buildSortedAllFieldMap(Map<Integer, List<Field>> orderFieldMap,
Map<Integer, Field> indexFiledMap) { Map<Integer, Field> indexFieldMap) {
Map<Integer, Field> sortedAllFiledMap = new HashMap<Integer, Field>( Map<Integer, Field> sortedAllFieldMap = new HashMap<Integer, Field>(
(orderFiledMap.size() + indexFiledMap.size()) * 4 / 3 + 1); (orderFieldMap.size() + indexFieldMap.size()) * 4 / 3 + 1);
Map<Integer, Field> tempIndexFiledMap = new HashMap<Integer, Field>(indexFiledMap); Map<Integer, Field> tempIndexFieldMap = new HashMap<Integer, Field>(indexFieldMap);
int index = 0; int index = 0;
for (List<Field> fieldList : orderFiledMap.values()) { for (List<Field> fieldList : orderFieldMap.values()) {
for (Field field : fieldList) { for (Field field : fieldList) {
while (tempIndexFiledMap.containsKey(index)) { while (tempIndexFieldMap.containsKey(index)) {
sortedAllFiledMap.put(index, tempIndexFiledMap.get(index)); sortedAllFieldMap.put(index, tempIndexFieldMap.get(index));
tempIndexFiledMap.remove(index); tempIndexFieldMap.remove(index);
index++; index++;
} }
sortedAllFiledMap.put(index, field); sortedAllFieldMap.put(index, field);
index++; index++;
} }
} }
sortedAllFiledMap.putAll(tempIndexFiledMap); sortedAllFieldMap.putAll(tempIndexFieldMap);
return sortedAllFiledMap; return sortedAllFieldMap;
} }
private static void declaredOneField(Field field, Map<Integer, List<Field>> orderFiledMap, private static void declaredOneField(Field field, Map<Integer, List<Field>> orderFieldMap,
Map<Integer, Field> indexFiledMap, Map<String, Field> ignoreMap, Map<Integer, Field> indexFieldMap, Map<String, Field> ignoreMap,
ExcelIgnoreUnannotated excelIgnoreUnannotated) { ExcelIgnoreUnannotated excelIgnoreUnannotated) {
ExcelIgnore excelIgnore = field.getAnnotation(ExcelIgnore.class); ExcelIgnore excelIgnore = field.getAnnotation(ExcelIgnore.class);
@ -330,11 +330,11 @@ public class ClassUtils {
return; return;
} }
if (excelProperty != null && excelProperty.index() >= 0) { if (excelProperty != null && excelProperty.index() >= 0) {
if (indexFiledMap.containsKey(excelProperty.index())) { if (indexFieldMap.containsKey(excelProperty.index())) {
throw new ExcelCommonException("The index of '" + indexFiledMap.get(excelProperty.index()).getName() throw new ExcelCommonException("The index of '" + indexFieldMap.get(excelProperty.index()).getName()
+ "' and '" + field.getName() + "' must be inconsistent"); + "' and '" + field.getName() + "' must be inconsistent");
} }
indexFiledMap.put(excelProperty.index(), field); indexFieldMap.put(excelProperty.index(), field);
return; return;
} }
@ -342,29 +342,29 @@ public class ClassUtils {
if (excelProperty != null) { if (excelProperty != null) {
order = excelProperty.order(); order = excelProperty.order();
} }
List<Field> orderFiledList = orderFiledMap.computeIfAbsent(order, key -> ListUtils.newArrayList()); List<Field> orderFieldList = orderFieldMap.computeIfAbsent(order, key -> ListUtils.newArrayList());
orderFiledList.add(field); orderFieldList.add(field);
} }
private static class FieldCache { private static class FieldCache {
private final Map<Integer, Field> sortedAllFiledMap; private final Map<Integer, Field> sortedAllFieldMap;
private final Map<Integer, Field> indexFiledMap; private final Map<Integer, Field> indexFieldMap;
private final Map<String, Field> ignoreMap; private final Map<String, Field> ignoreMap;
public FieldCache(Map<Integer, Field> sortedAllFiledMap, Map<Integer, Field> indexFiledMap, public FieldCache(Map<Integer, Field> sortedAllFieldMap, Map<Integer, Field> indexFieldMap,
Map<String, Field> ignoreMap) { Map<String, Field> ignoreMap) {
this.sortedAllFiledMap = sortedAllFiledMap; this.sortedAllFieldMap = sortedAllFieldMap;
this.indexFiledMap = indexFiledMap; this.indexFieldMap = indexFieldMap;
this.ignoreMap = ignoreMap; this.ignoreMap = ignoreMap;
} }
public Map<Integer, Field> getSortedAllFiledMap() { public Map<Integer, Field> getSortedAllFieldMap() {
return sortedAllFiledMap; return sortedAllFieldMap;
} }
public Map<Integer, Field> getIndexFiledMap() { public Map<Integer, Field> getIndexFieldMap() {
return indexFiledMap; return indexFieldMap;
} }
public Map<String, Field> getIgnoreMap() { public Map<String, Field> getIgnoreMap() {

4
easyexcel-core/src/main/java/com/alibaba/excel/util/FieldUtils.java

@ -30,9 +30,9 @@ public class FieldUtils {
private static final int START_RESOLVE_FIELD_LENGTH = 2; private static final int START_RESOLVE_FIELD_LENGTH = 2;
public static Class<?> getFieldClass(Map dataMap, String filedName, Object value) { public static Class<?> getFieldClass(Map dataMap, String fieldName, Object value) {
if (dataMap instanceof BeanMap) { if (dataMap instanceof BeanMap) {
Class<?> fieldClass = ((BeanMap)dataMap).getPropertyType(filedName); Class<?> fieldClass = ((BeanMap)dataMap).getPropertyType(fieldName);
if (fieldClass != null) { if (fieldClass != null) {
return fieldClass; return fieldClass;
} }

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

@ -77,11 +77,21 @@ public abstract class AbstractExcelWriterParameterBuilder<T extends AbstractExce
return self(); return self();
} }
/**
* Ignore the custom columns.
*
* @deprecated use {@link #excludeColumnFieldNames(Collection)}
*/
public T excludeColumnFiledNames(Collection<String> excludeColumnFieldNames) {
parameter().setExcludeColumnFieldNames(excludeColumnFieldNames);
return self();
}
/** /**
* Ignore the custom columns. * Ignore the custom columns.
*/ */
public T excludeColumnFiledNames(Collection<String> excludeColumnFiledNames) { public T excludeColumnFieldNames(Collection<String> excludeColumnFieldNames) {
parameter().setExcludeColumnFieldNames(excludeColumnFiledNames); parameter().setExcludeColumnFieldNames(excludeColumnFieldNames);
return self(); return self();
} }
@ -95,10 +105,19 @@ public abstract class AbstractExcelWriterParameterBuilder<T extends AbstractExce
/** /**
* Only output the custom columns. * Only output the custom columns.
*
* @deprecated use {@link #includeColumnFieldNames(Collection)}
*/ */
public T includeColumnFiledNames(Collection<String> includeColumnFiledNames) { public T includeColumnFiledNames(Collection<String> includeColumnFieldNames) {
parameter().setIncludeColumnFieldNames(includeColumnFiledNames); parameter().setIncludeColumnFieldNames(includeColumnFieldNames);
return self(); return self();
} }
/**
* Only output the custom columns.
*/
public T includeColumnFieldNames(Collection<String> includeColumnFieldNames) {
parameter().setIncludeColumnFieldNames(includeColumnFieldNames);
return self();
}
} }

34
easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -50,18 +50,18 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
if (writeSheetHolder.isNew() && !writeSheetHolder.getExcelWriteHeadProperty().hasHead()) { if (writeSheetHolder.isNew() && !writeSheetHolder.getExcelWriteHeadProperty().hasHead()) {
newRowIndex += writeContext.currentWriteHolder().relativeHeadRowIndex(); newRowIndex += writeContext.currentWriteHolder().relativeHeadRowIndex();
} }
// BeanMap is out of order, so use sortedAllFiledMap // BeanMap is out of order, so use sortedAllFieldMap
Map<Integer, Field> sortedAllFiledMap = new TreeMap<>(); Map<Integer, Field> sortedAllFieldMap = new TreeMap<>();
int relativeRowIndex = 0; int relativeRowIndex = 0;
for (Object oneRowData : data) { for (Object oneRowData : data) {
int lastRowIndex = relativeRowIndex + newRowIndex; int lastRowIndex = relativeRowIndex + newRowIndex;
addOneRowOfDataToExcel(oneRowData, lastRowIndex, relativeRowIndex, sortedAllFiledMap); addOneRowOfDataToExcel(oneRowData, lastRowIndex, relativeRowIndex, sortedAllFieldMap);
relativeRowIndex++; relativeRowIndex++;
} }
} }
private void addOneRowOfDataToExcel(Object oneRowData, int rowIndex, int relativeRowIndex, private void addOneRowOfDataToExcel(Object oneRowData, int rowIndex, int relativeRowIndex,
Map<Integer, Field> sortedAllFiledMap) { Map<Integer, Field> sortedAllFieldMap) {
if (oneRowData == null) { if (oneRowData == null) {
return; return;
} }
@ -79,7 +79,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
} else if (oneRowData instanceof Map) { } else if (oneRowData instanceof Map) {
addBasicTypeToExcel(new MapRowData((Map<Integer, ?>)oneRowData), row, rowIndex, relativeRowIndex); addBasicTypeToExcel(new MapRowData((Map<Integer, ?>)oneRowData), row, rowIndex, relativeRowIndex);
} else { } else {
addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap); addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFieldMap);
} }
WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext); WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext);
@ -139,7 +139,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
} }
private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex, private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex,
Map<Integer, Field> sortedAllFiledMap) { Map<Integer, Field> sortedAllFieldMap) {
WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
BeanMap beanMap = BeanMapUtils.create(oneRowData); BeanMap beanMap = BeanMapUtils.create(oneRowData);
// Bean the contains of the Map Key method with poor performance,So to create a keySet here // Bean the contains of the Map Key method with poor performance,So to create a keySet here
@ -186,18 +186,18 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
maxCellIndex++; maxCellIndex++;
Map<String, Field> ignoreMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getIgnoreMap(); Map<String, Field> ignoreMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getIgnoreMap();
initSortedAllFiledMapFieldList(oneRowData.getClass(), sortedAllFiledMap); initSortedAllFieldMapFieldList(oneRowData.getClass(), sortedAllFieldMap);
for (Map.Entry<Integer, Field> entry : sortedAllFiledMap.entrySet()) { for (Map.Entry<Integer, Field> entry : sortedAllFieldMap.entrySet()) {
Field field = entry.getValue(); Field field = entry.getValue();
String filedName = FieldUtils.resolveCglibFieldName(field); String fieldName = FieldUtils.resolveCglibFieldName(field);
boolean uselessData = !beanKeySet.contains(filedName) || beanMapHandledSet.contains(filedName) boolean uselessData = !beanKeySet.contains(fieldName) || beanMapHandledSet.contains(fieldName)
|| ignoreMap.containsKey(filedName); || ignoreMap.containsKey(fieldName);
if (uselessData) { if (uselessData) {
continue; continue;
} }
Object value = beanMap.get(filedName); Object value = beanMap.get(fieldName);
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap,
currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), filedName); currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), fieldName);
CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(
writeContext, row, rowIndex, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); writeContext, row, rowIndex, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty);
WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
@ -210,7 +210,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
cellWriteHandlerContext.setOriginalValue(value); cellWriteHandlerContext.setOriginalValue(value);
cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(beanMap, filedName, value)); cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(beanMap, fieldName, value));
converterAndSet(cellWriteHandlerContext); converterAndSet(cellWriteHandlerContext);
WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
@ -218,8 +218,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
} }
} }
private void initSortedAllFiledMapFieldList(Class<?> clazz, Map<Integer, Field> sortedAllFiledMap) { private void initSortedAllFieldMapFieldList(Class<?> clazz, Map<Integer, Field> sortedAllFieldMap) {
if (!sortedAllFiledMap.isEmpty()) { if (!sortedAllFieldMap.isEmpty()) {
return; return;
} }
@ -229,7 +229,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
.isEmpty(writeSheetHolder.getExcludeColumnIndexes()) || !CollectionUtils .isEmpty(writeSheetHolder.getExcludeColumnIndexes()) || !CollectionUtils
.isEmpty(writeSheetHolder.getIncludeColumnFieldNames()) || !CollectionUtils .isEmpty(writeSheetHolder.getIncludeColumnFieldNames()) || !CollectionUtils
.isEmpty(writeSheetHolder.getIncludeColumnIndexes()); .isEmpty(writeSheetHolder.getIncludeColumnIndexes());
ClassUtils.declaredFields(clazz, sortedAllFiledMap, needIgnore, writeSheetHolder); ClassUtils.declaredFields(clazz, sortedAllFieldMap, needIgnore, writeSheetHolder);
} }
} }

70
easyexcel-core/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeDataTest.java

@ -25,30 +25,30 @@ public class ExcludeOrIncludeDataTest {
private static File excludeIndex07; private static File excludeIndex07;
private static File excludeIndex03; private static File excludeIndex03;
private static File excludeIndexCsv; private static File excludeIndexCsv;
private static File excludeFiledName07; private static File excludeFieldName07;
private static File excludeFiledName03; private static File excludeFieldName03;
private static File excludeFiledNameCsv; private static File excludeFieldNameCsv;
private static File includeIndex07; private static File includeIndex07;
private static File includeIndex03; private static File includeIndex03;
private static File includeIndexCsv; private static File includeIndexCsv;
private static File includeFiledName07; private static File includeFieldName07;
private static File includeFiledName03; private static File includeFieldName03;
private static File includeFiledNameCsv; private static File includeFieldNameCsv;
@BeforeClass @BeforeClass
public static void init() { public static void init() {
excludeIndex07 = TestFileUtil.createNewFile("excludeIndex.xlsx"); excludeIndex07 = TestFileUtil.createNewFile("excludeIndex.xlsx");
excludeIndex03 = TestFileUtil.createNewFile("excludeIndex.xls"); excludeIndex03 = TestFileUtil.createNewFile("excludeIndex.xls");
excludeIndexCsv = TestFileUtil.createNewFile("excludeIndex.csv"); excludeIndexCsv = TestFileUtil.createNewFile("excludeIndex.csv");
excludeFiledName07 = TestFileUtil.createNewFile("excludeFiledName.xlsx"); excludeFieldName07 = TestFileUtil.createNewFile("excludeFieldName.xlsx");
excludeFiledName03 = TestFileUtil.createNewFile("excludeFiledName.xls"); excludeFieldName03 = TestFileUtil.createNewFile("excludeFieldName.xls");
excludeFiledNameCsv = TestFileUtil.createNewFile("excludeFiledName.csv"); excludeFieldNameCsv = TestFileUtil.createNewFile("excludeFieldName.csv");
includeIndex07 = TestFileUtil.createNewFile("includeIndex.xlsx"); includeIndex07 = TestFileUtil.createNewFile("includeIndex.xlsx");
includeIndex03 = TestFileUtil.createNewFile("includeIndex.xls"); includeIndex03 = TestFileUtil.createNewFile("includeIndex.xls");
includeIndexCsv = TestFileUtil.createNewFile("includeIndex.csv"); includeIndexCsv = TestFileUtil.createNewFile("includeIndex.csv");
includeFiledName07 = TestFileUtil.createNewFile("includeFiledName.xlsx"); includeFieldName07 = TestFileUtil.createNewFile("includeFieldName.xlsx");
includeFiledName03 = TestFileUtil.createNewFile("includeFiledName.xls"); includeFieldName03 = TestFileUtil.createNewFile("includeFieldName.xls");
includeFiledNameCsv = TestFileUtil.createNewFile("includeFiledName.csv"); includeFieldNameCsv = TestFileUtil.createNewFile("includeFieldName.csv");
} }
@Test @Test
@ -67,18 +67,18 @@ public class ExcludeOrIncludeDataTest {
} }
@Test @Test
public void t11ExcludeFiledName07() { public void t11ExcludeFieldName07() {
excludeFiledName(excludeFiledName07); excludeFieldName(excludeFieldName07);
} }
@Test @Test
public void t12ExcludeFiledName03() { public void t12ExcludeFieldName03() {
excludeFiledName(excludeFiledName03); excludeFieldName(excludeFieldName03);
} }
@Test @Test
public void t13ExcludeFiledNameCsv() { public void t13ExcludeFieldNameCsv() {
excludeFiledName(excludeFiledNameCsv); excludeFieldName(excludeFieldNameCsv);
} }
@ -98,18 +98,18 @@ public class ExcludeOrIncludeDataTest {
} }
@Test @Test
public void t31IncludeFiledName07() { public void t31IncludeFieldName07() {
includeFiledName(includeFiledName07); includeFieldName(includeFieldName07);
} }
@Test @Test
public void t32IncludeFiledName03() { public void t32IncludeFieldName03() {
includeFiledName(includeFiledName03); includeFieldName(includeFieldName03);
} }
@Test @Test
public void t33IncludeFiledNameCsv() { public void t33IncludeFieldNameCsv() {
includeFiledName(includeFiledNameCsv); includeFieldName(includeFieldNameCsv);
} }
private void excludeIndex(File file) { private void excludeIndex(File file) {
@ -127,12 +127,12 @@ public class ExcludeOrIncludeDataTest {
} }
private void excludeFiledName(File file) { private void excludeFieldName(File file) {
Set<String> excludeColumnFiledNames = new HashSet<String>(); Set<String> excludeColumnFieldNames = new HashSet<String>();
excludeColumnFiledNames.add("column1"); excludeColumnFieldNames.add("column1");
excludeColumnFiledNames.add("column3"); excludeColumnFieldNames.add("column3");
excludeColumnFiledNames.add("column4"); excludeColumnFieldNames.add("column4");
EasyExcel.write(file, ExcludeOrIncludeData.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet() EasyExcel.write(file, ExcludeOrIncludeData.class).excludeColumnFieldNames(excludeColumnFieldNames).sheet()
.doWrite(data()); .doWrite(data());
List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync(); List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync();
Assert.assertEquals(1, dataMap.size()); Assert.assertEquals(1, dataMap.size());
@ -157,11 +157,11 @@ public class ExcludeOrIncludeDataTest {
} }
private void includeFiledName(File file) { private void includeFieldName(File file) {
Set<String> includeColumnFiledNames = new HashSet<String>(); Set<String> includeColumnFieldNames = new HashSet<String>();
includeColumnFiledNames.add("column2"); includeColumnFieldNames.add("column2");
includeColumnFiledNames.add("column3"); includeColumnFieldNames.add("column3");
EasyExcel.write(file, ExcludeOrIncludeData.class).includeColumnFiledNames(includeColumnFiledNames).sheet() EasyExcel.write(file, ExcludeOrIncludeData.class).includeColumnFieldNames(includeColumnFieldNames).sheet()
.doWrite(data()); .doWrite(data());
List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync(); List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync();
Assert.assertEquals(1, dataMap.size()); Assert.assertEquals(1, dataMap.size());

12
easyexcel-core/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

@ -119,18 +119,18 @@ public class WriteTest {
// 这里需要注意 在使用ExcelProperty注解的使用,如果想不空列则需要加入order字段,而不是index,order会忽略空列,然后继续往后,而index,不会忽略空列,在第几列就是第几列。 // 这里需要注意 在使用ExcelProperty注解的使用,如果想不空列则需要加入order字段,而不是index,order会忽略空列,然后继续往后,而index,不会忽略空列,在第几列就是第几列。
// 根据用户传入字段 假设我们要忽略 date // 根据用户传入字段 假设我们要忽略 date
Set<String> excludeColumnFiledNames = new HashSet<>(); Set<String> excludeColumnFieldNames = new HashSet<>();
excludeColumnFiledNames.add("date"); excludeColumnFieldNames.add("date");
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet("模板") EasyExcel.write(fileName, DemoData.class).excludeColumnFieldNames(excludeColumnFieldNames).sheet("模板")
.doWrite(data()); .doWrite(data());
fileName = TestFileUtil.getPath() + "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx"; fileName = TestFileUtil.getPath() + "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx";
// 根据用户传入字段 假设我们只要导出 date // 根据用户传入字段 假设我们只要导出 date
Set<String> includeColumnFiledNames = new HashSet<>(); Set<String> includeColumnFieldNames = new HashSet<>();
includeColumnFiledNames.add("date"); includeColumnFieldNames.add("date");
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板") EasyExcel.write(fileName, DemoData.class).includeColumnFieldNames(includeColumnFieldNames).sheet("模板")
.doWrite(data()); .doWrite(data());
} }

10
easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java

@ -164,18 +164,18 @@ public class StyleTest {
public void testFormatter2() throws Exception { public void testFormatter2() throws Exception {
StyleData styleData = new StyleData(); StyleData styleData = new StyleData();
Field field = styleData.getClass().getDeclaredField("byteValue"); Field field = styleData.getClass().getDeclaredField("byteValue");
LOGGER.info("filed:{}", field.getType().getName()); LOGGER.info("field:{}", field.getType().getName());
field = styleData.getClass().getDeclaredField("byteValue2"); field = styleData.getClass().getDeclaredField("byteValue2");
LOGGER.info("filed:{}", field.getType().getName()); LOGGER.info("field:{}", field.getType().getName());
field = styleData.getClass().getDeclaredField("byteValue4"); field = styleData.getClass().getDeclaredField("byteValue4");
LOGGER.info("filed:{}", field.getType()); LOGGER.info("field:{}", field.getType());
field = styleData.getClass().getDeclaredField("byteValue3"); field = styleData.getClass().getDeclaredField("byteValue3");
LOGGER.info("filed:{}", field.getType()); LOGGER.info("field:{}", field.getType());
} }
@Test @Test
public void testFormatter3() throws Exception { public void testFormatter3() throws Exception {
LOGGER.info("filed:{}", Byte.class == Byte.class); LOGGER.info("field:{}", Byte.class == Byte.class);
} }
private void isDate(Cell cell) { private void isDate(Cell cell) {

1
update.md

@ -3,6 +3,7 @@
* 在有样式没有数据的情况下也算空行 [Issue #2294](https://github.com/alibaba/easyexcel/issues/2294) * 在有样式没有数据的情况下也算空行 [Issue #2294](https://github.com/alibaba/easyexcel/issues/2294)
* 修复无法根据文件流判断csv的bug [Issue #2297](https://github.com/alibaba/easyexcel/issues/2297) * 修复无法根据文件流判断csv的bug [Issue #2297](https://github.com/alibaba/easyexcel/issues/2297)
* 修复CSV不关闭流的bug [Issue #2309](https://github.com/alibaba/easyexcel/issues/2309) * 修复CSV不关闭流的bug [Issue #2309](https://github.com/alibaba/easyexcel/issues/2309)
* 修复`filed`拼接错误 [Issue #2390](https://github.com/alibaba/easyexcel/issues/2390)
# 3.0.5 # 3.0.5

Loading…
Cancel
Save