提交 c6915a46 编写于 作者: 庄家钜's avatar 庄家钜

修复无对象读 返回map的size可能会头的size不一致 [Issue #2014]

上级 d3f6cbbf
......@@ -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();
......
......@@ -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);
......
......@@ -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<Object> list = EasyExcel.read(file).sheet(0).headRowNumber(0).doReadSync();
List<Object> 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 =
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册