|
|
|
@ -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<Map<Integer, CellData>> { |
|
|
|
|
private int headSize; |
|
|
|
|
public class ModelBuildEventListener implements ReadListener<Map<Integer, ReadCellData<?>>> { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void invokeHead(Map<Integer, CellData> cellDataMap, AnalysisContext context) { |
|
|
|
|
this.headSize = cellDataMap.size(); |
|
|
|
|
public void invokeHead(Map<Integer, ReadCellData<?>> 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<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder, |
|
|
|
|
AnalysisContext context) { |
|
|
|
|
int index = 0; |
|
|
|
|
if (context.readWorkbookHolder().getDefaultReturnMap()) { |
|
|
|
|
Map<Integer, String> map = new LinkedHashMap<Integer, String>(cellDataMap.size() * 4 / 3 + 1); |
|
|
|
|
for (Map.Entry<Integer, CellData> 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<Integer, String> map = MapUtils.newLinkedHashMapWithExpectedSize(cellDataMap.size()); |
|
|
|
|
for (Map.Entry<Integer, ReadCellData<?>> 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<String> list = new ArrayList<String>(); |
|
|
|
|
for (Map.Entry<Integer, CellData> 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<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder, |
|
|
|
|
AnalysisContext context) { |
|
|
|
|
ExcelReadHeadProperty excelReadHeadProperty = readSheetHolder.excelReadHeadProperty(); |
|
|
|
|