From c6915a469c5f2136986888c5ec56810f2dac3f74 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 15 Sep 2021 16:38:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E8=AF=BB=20=E8=BF=94=E5=9B=9Emap=E7=9A=84size=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E4=BC=9A=E5=A4=B4=E7=9A=84size=E4=B8=8D=E4=B8=80?= =?UTF-8?q?=E8=87=B4=20[Issue=20#2014]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/ModelBuildEventListener.java | 85 +++++++------------ .../read/metadata/holder/ReadSheetHolder.java | 5 ++ .../easyexcel/test/temp/Lock2Test.java | 27 +++--- update.md | 1 + 4 files changed, 53 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index f56eb26c..6a814f4d 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -16,17 +16,21 @@ import com.alibaba.excel.util.ConverterUtils; import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.MapUtils; +import org.apache.commons.collections4.CollectionUtils; + /** * Convert to the object the user needs * * @author jipengfei */ -public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener> { - private int headSize; +public class ModelBuildEventListener implements ReadListener>> { @Override - public void invokeHead(Map cellDataMap, AnalysisContext context) { - this.headSize = cellDataMap.size(); + 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 @@ -43,61 +47,38 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener private Object buildStringList(Map> cellDataMap, ReadSheetHolder readSheetHolder, AnalysisContext context) { int index = 0; - if (context.readWorkbookHolder().getDefaultReturnMap()) { - Map map = new LinkedHashMap(cellDataMap.size() * 4 / 3 + 1); - for (Map.Entry entry : cellDataMap.entrySet()) { - Integer key = entry.getKey(); - CellData cellData = entry.getValue(); - while (index < key) { - map.put(index, null); - index++; - } - index++; - if (cellData.getType() == CellDataTypeEnum.EMPTY) { - map.put(key, null); - continue; - } - map.put(key, - (String)ConverterUtils.convertToJavaObject(cellData, null, null, currentReadHolder.converterMap(), - currentReadHolder.globalConfiguration(), context.readRowHolder().getRowIndex(), key)); - } - int headSize = currentReadHolder.excelReadHeadProperty().getHeadMap().size() == 0 - ? this.headSize : currentReadHolder.excelReadHeadProperty().getHeadMap().size(); - while (index < headSize) { + Map map = MapUtils.newLinkedHashMapWithExpectedSize(cellDataMap.size()); + for (Map.Entry> entry : cellDataMap.entrySet()) { + Integer key = entry.getKey(); + ReadCellData cellData = entry.getValue(); + while (index < key) { map.put(index, null); index++; } - return map; - } else { - // Compatible with the old code the old code returns a list - List list = new ArrayList(); - for (Map.Entry entry : cellDataMap.entrySet()) { - Integer key = entry.getKey(); - CellData cellData = entry.getValue(); - while (index < key) { - list.add(null); - index++; - } - index++; - if (cellData.getType() == CellDataTypeEnum.EMPTY) { - list.add(null); - continue; - } - list.add( - (String)ConverterUtils.convertToJavaObject(cellData, null, null, currentReadHolder.converterMap(), - currentReadHolder.globalConfiguration(), context.readRowHolder().getRowIndex(), key)); - } - int headSize = currentReadHolder.excelReadHeadProperty().getHeadMap().size() == 0 - ? this.headSize : currentReadHolder.excelReadHeadProperty().getHeadMap().size(); - while (index < headSize) { - list.add(null); - index++; - } - return list; + index++; + map.put(key, + (String)ConverterUtils.convertToJavaObject(cellData, null, null, readSheetHolder.converterMap(), + context, context.readRowHolder().getRowIndex(), key)); + } + // fix https://github.com/alibaba/easyexcel/issues/2014 + int headSize = calculateHeadSize(readSheetHolder); + while (index < headSize) { + map.put(index, null); + index++; } return map; } + private int calculateHeadSize(ReadSheetHolder readSheetHolder) { + if (readSheetHolder.excelReadHeadProperty().getHeadMap().size() > 0) { + return readSheetHolder.excelReadHeadProperty().getHeadMap().size(); + } + if (readSheetHolder.getMaxDataHeadSize() != null) { + return readSheetHolder.getMaxDataHeadSize(); + } + return 0; + } + private Object buildUserModel(Map> cellDataMap, ReadSheetHolder readSheetHolder, AnalysisContext context) { ExcelReadHeadProperty excelReadHeadProperty = readSheetHolder.excelReadHeadProperty(); diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java index 12eb5dc4..61b17587 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java @@ -57,6 +57,11 @@ public class ReadSheetHolder extends AbstractReadHolder { * Current CellData */ private ReadCellData tempCellData; + /** + * Read the size of the largest head in sheet head data. + * see https://github.com/alibaba/easyexcel/issues/2014 + */ + private Integer maxDataHeadSize; public ReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { super(readSheet, readWorkbookHolder); diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java index e8545670..94f8ca31 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java @@ -5,14 +5,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import org.apache.poi.hssf.util.CellReference; -import org.apache.poi.ss.usermodel.FillPatternType; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.junit.Ignore; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; @@ -21,6 +13,15 @@ import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import com.alibaba.fastjson.JSON; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.poi.hssf.util.CellReference; +import org.apache.poi.ss.usermodel.FillPatternType; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * 临时测试 * @@ -33,12 +34,12 @@ public class Lock2Test { @Test public void test() throws Exception { -// File file = TestFileUtil.readUserHomeFile("test/test6.xls"); - File file = new File("/Users/zhuangjiaju/Downloads/1.xlsx"); + File file = TestFileUtil.readUserHomeFile("test/test4.xlsx"); - List list = EasyExcel.read(file).sheet(0).headRowNumber(0).doReadSync(); + List list = EasyExcel.read(file).sheet(0).doReadSync(); LOGGER.info("数据:{}", list.size()); for (Object data : list) { + LOGGER.info("返回数据:{}", CollectionUtils.size(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data)); } } @@ -60,7 +61,7 @@ public class Lock2Test { // 背景设置为红色 headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); WriteFont headWriteFont = new WriteFont(); - headWriteFont.setFontHeightInPoints((short) 20); + headWriteFont.setFontHeightInPoints((short)20); headWriteCellStyle.setWriteFont(headWriteFont); // 内容的策略 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); @@ -70,7 +71,7 @@ public class Lock2Test { contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); WriteFont contentWriteFont = new WriteFont(); // 字体大小 - contentWriteFont.setFontHeightInPoints((short) 20); + contentWriteFont.setFontHeightInPoints((short)20); contentWriteCellStyle.setWriteFont(contentWriteFont); // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现 HorizontalCellStyleStrategy horizontalCellStyleStrategy = diff --git a/update.md b/update.md index 23aef9c6..7192df0d 100644 --- a/update.md +++ b/update.md @@ -17,6 +17,7 @@ * `Converter`支持null转换 [Issue #1776](https://github.com/alibaba/easyexcel/issues/1776) * cglib 新增命名策略,防止和`spring`的冲突 [Issue #2064](https://github.com/alibaba/easyexcel/issues/2064) * 修改填充可能填充错误的bug [Issue #2035](https://github.com/alibaba/easyexcel/issues/2035) +* 修复无对象读 返回map的size可能会头的size不一致 [Issue #2014](https://github.com/alibaba/easyexcel/issues/2014) # 2.2.10 * 修复读取的时候用string接收数字 可能四舍五入不一致的bug