diff --git a/README.md b/README.md index 292bac65..f4671022 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都 com.alibaba easyexcel - 3.1.3 + 3.1.4 ``` diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index 44f77513..a313acb3 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -25,14 +25,6 @@ import org.springframework.cglib.beans.BeanMap; */ public class ModelBuildEventListener implements IgnoreExceptionReadListener>> { - @Override - public void invokeHead(Map> cellDataMap, AnalysisContext context) { - if (context.readSheetHolder().getMaxDataHeadSize() == null - || context.readSheetHolder().getMaxDataHeadSize() < CollectionUtils.size(cellDataMap)) { - context.readSheetHolder().setMaxDataHeadSize(CollectionUtils.size(cellDataMap)); - } - } - @Override public void invoke(Map> cellDataMap, AnalysisContext context) { ReadSheetHolder readSheetHolder = context.readSheetHolder(); @@ -73,8 +65,8 @@ public class ModelBuildEventListener implements IgnoreExceptionReadListener 0) { return readSheetHolder.excelReadHeadProperty().getHeadMap().size(); } - if (readSheetHolder.getMaxDataHeadSize() != null) { - return readSheetHolder.getMaxDataHeadSize(); + if (readSheetHolder.getMaxNotEmptyDataHeadSize() != null) { + return readSheetHolder.getMaxNotEmptyDataHeadSize(); } return 0; } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java index 197835ad..47957044 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java @@ -65,7 +65,7 @@ public class ReadSheetHolder extends AbstractReadHolder { * Read the size of the largest head in sheet head data. * see https://github.com/alibaba/easyexcel/issues/2014 */ - private Integer maxDataHeadSize; + private Integer maxNotEmptyDataHeadSize; public ReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { super(readSheet, readWorkbookHolder); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java index 45edb7b7..642e71fb 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java @@ -3,8 +3,11 @@ package com.alibaba.excel.read.processor; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.exception.ExcelAnalysisException; @@ -17,6 +20,8 @@ import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; import com.alibaba.excel.util.ConverterUtils; import com.alibaba.excel.util.StringUtils; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -111,6 +116,14 @@ public class DefaultAnalysisEventProcessor implements AnalysisEventProcessor { } private void buildHead(AnalysisContext analysisContext, Map> cellDataMap) { + // Rule out empty head, and then take the largest column + if (MapUtils.isNotEmpty(cellDataMap)) { + cellDataMap.entrySet() + .stream() + .filter(entry -> CellDataTypeEnum.EMPTY != entry.getValue().getType()) + .forEach(entry -> analysisContext.readSheetHolder().setMaxNotEmptyDataHeadSize(entry.getKey())); + } + if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) { return; } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java index 3844978f..3b0b525a 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java @@ -42,4 +42,14 @@ public class CompatibilityTest { Map row2 = list.get(2); Assert.assertEquals("1,2-戊二醇", row2.get(2)); } + + @Test + public void t03() { + // Exist in `sharedStrings.xml` `x:t` start tag, need to be compatible + List> list = EasyExcel.read(TestFileUtil.getPath() + "compatibility/t03.xlsx").sheet().doReadSync(); + log.info("data:{}", JSON.toJSONString(list)); + Assert.assertEquals(1, list.size()); + Map row0 = list.get(0); + Assert.assertEquals(12, row0.size()); + } } diff --git a/easyexcel-test/src/test/resources/compatibility/t03.xlsx b/easyexcel-test/src/test/resources/compatibility/t03.xlsx new file mode 100644 index 00000000..3a31ef78 Binary files /dev/null and b/easyexcel-test/src/test/resources/compatibility/t03.xlsx differ diff --git a/pom.xml b/pom.xml index 96d7f698..eca83ca2 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ - 3.1.3 + 3.1.4 UTF-8 1.8 true diff --git a/update.md b/update.md index bc18ccf1..2c698a0b 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,7 @@ +# 3.1.4 + +* 提高xlsx读取兼容性:在存在第一行很多空列的情况下,忽略空列 + # 3.1.3 * 提高xlsx兼容性:兼容`sharedStrings.xml` 存在 `x:t`标签的情况 @@ -156,7 +160,9 @@ * 发布正式版 * 修复第一行为空不会调用`invokeHeadMap`的bug [Issue #993](https://github.com/alibaba/easyexcel/issues/993) * + 当类的属性没有按照ExcelProperty的属性index顺序排序的时候,写数据出现错乱 [Issue #1046](https://github.com/alibaba/easyexcel/issues/1046) + * 新增支持自定义转换器 入参可以为空 实现`NullableObjectConverter` 即可 [Issue #1084](https://github.com/alibaba/easyexcel/issues/1084) * 修复xls丢失结束标记的情况下 会漏读最后一行