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 508a8a4b..f56eb26c 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -21,10 +21,13 @@ import com.alibaba.excel.util.MapUtils; * * @author jipengfei */ -public class ModelBuildEventListener implements ReadListener>> { +public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener> { + private int headSize; @Override - public void invokeHead(Map> cellDataMap, AnalysisContext context) {} + public void invokeHead(Map cellDataMap, AnalysisContext context) { + this.headSize = cellDataMap.size(); + } @Override public void invoke(Map> cellDataMap, AnalysisContext context) { @@ -40,23 +43,57 @@ public class ModelBuildEventListener implements ReadListener> cellDataMap, ReadSheetHolder readSheetHolder, AnalysisContext context) { int index = 0; - Map map = MapUtils.newLinkedHashMapWithExpectedSize(cellDataMap.size()); - for (Map.Entry> entry : cellDataMap.entrySet()) { - Integer key = entry.getKey(); - ReadCellData cellData = entry.getValue(); - while (index < key) { + 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.put(index, null); index++; } - index++; - map.put(key, - (String)ConverterUtils.convertToJavaObject(cellData, null, null, readSheetHolder.converterMap(), - context, context.readRowHolder().getRowIndex(), key)); - } - int headSize = readSheetHolder.excelReadHeadProperty().getHeadMap().size(); - while (index < headSize) { - 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; } return map; }