Browse Source

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

developing
Jiaju Zhuang 3 years ago
parent
commit
c6915a469c
  1. 63
      src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
  2. 5
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java
  3. 27
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  4. 1
      update.md

63
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.FieldUtils;
import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
/** /**
* Convert to the object the user needs * Convert to the object the user needs
* *
* @author jipengfei * @author jipengfei
*/ */
public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener<Map<Integer, CellData>> { public class ModelBuildEventListener implements ReadListener<Map<Integer, ReadCellData<?>>> {
private int headSize;
@Override @Override
public void invokeHead(Map<Integer, CellData> cellDataMap, AnalysisContext context) { public void invokeHead(Map<Integer, ReadCellData<?>> cellDataMap, AnalysisContext context) {
this.headSize = cellDataMap.size(); if (context.readSheetHolder().getMaxDataHeadSize() == null
|| context.readSheetHolder().getMaxDataHeadSize() < CollectionUtils.size(cellDataMap)) {
context.readSheetHolder().setMaxDataHeadSize(CollectionUtils.size(cellDataMap));
}
} }
@Override @Override
@ -43,59 +47,36 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
private Object buildStringList(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder, private Object buildStringList(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder,
AnalysisContext context) { AnalysisContext context) {
int index = 0; int index = 0;
if (context.readWorkbookHolder().getDefaultReturnMap()) { Map<Integer, String> map = MapUtils.newLinkedHashMapWithExpectedSize(cellDataMap.size());
Map<Integer, String> map = new LinkedHashMap<Integer, String>(cellDataMap.size() * 4 / 3 + 1); for (Map.Entry<Integer, ReadCellData<?>> entry : cellDataMap.entrySet()) {
for (Map.Entry<Integer, CellData> entry : cellDataMap.entrySet()) {
Integer key = entry.getKey(); Integer key = entry.getKey();
CellData cellData = entry.getValue(); ReadCellData<?> cellData = entry.getValue();
while (index < key) { while (index < key) {
map.put(index, null); map.put(index, null);
index++; index++;
} }
index++; index++;
if (cellData.getType() == CellDataTypeEnum.EMPTY) {
map.put(key, null);
continue;
}
map.put(key, map.put(key,
(String)ConverterUtils.convertToJavaObject(cellData, null, null, currentReadHolder.converterMap(), (String)ConverterUtils.convertToJavaObject(cellData, null, null, readSheetHolder.converterMap(),
currentReadHolder.globalConfiguration(), context.readRowHolder().getRowIndex(), key)); context, context.readRowHolder().getRowIndex(), key));
} }
int headSize = currentReadHolder.excelReadHeadProperty().getHeadMap().size() == 0 // fix https://github.com/alibaba/easyexcel/issues/2014
? this.headSize : currentReadHolder.excelReadHeadProperty().getHeadMap().size(); int headSize = calculateHeadSize(readSheetHolder);
while (index < headSize) { while (index < headSize) {
map.put(index, null); map.put(index, null);
index++; index++;
} }
return map; 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) { private int calculateHeadSize(ReadSheetHolder readSheetHolder) {
list.add(null); if (readSheetHolder.excelReadHeadProperty().getHeadMap().size() > 0) {
continue; return readSheetHolder.excelReadHeadProperty().getHeadMap().size();
}
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; if (readSheetHolder.getMaxDataHeadSize() != null) {
return readSheetHolder.getMaxDataHeadSize();
} }
return map; return 0;
} }
private Object buildUserModel(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder, private Object buildUserModel(Map<Integer, ReadCellData<?>> cellDataMap, ReadSheetHolder readSheetHolder,

5
src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java

@ -57,6 +57,11 @@ public class ReadSheetHolder extends AbstractReadHolder {
* Current CellData * Current CellData
*/ */
private ReadCellData<?> tempCellData; 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) { public ReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) {
super(readSheet, readWorkbookHolder); super(readSheet, readWorkbookHolder);

27
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.Date;
import java.util.List; 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.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; 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.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON; 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 @Test
public void test() throws Exception { public void test() throws Exception {
// File file = TestFileUtil.readUserHomeFile("test/test6.xls"); File file = TestFileUtil.readUserHomeFile("test/test4.xlsx");
File file = new File("/Users/zhuangjiaju/Downloads/1.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()); LOGGER.info("数据:{}", list.size());
for (Object data : list) { for (Object data : list) {
LOGGER.info("返回数据:{}", CollectionUtils.size(data));
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data));
} }
} }
@ -60,7 +61,7 @@ public class Lock2Test {
// 背景设置为红色 // 背景设置为红色
headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
WriteFont headWriteFont = new WriteFont(); WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 20); headWriteFont.setFontHeightInPoints((short)20);
headWriteCellStyle.setWriteFont(headWriteFont); headWriteCellStyle.setWriteFont(headWriteFont);
// 内容的策略 // 内容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
@ -70,7 +71,7 @@ public class Lock2Test {
contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
WriteFont contentWriteFont = new WriteFont(); WriteFont contentWriteFont = new WriteFont();
// 字体大小 // 字体大小
contentWriteFont.setFontHeightInPoints((short) 20); contentWriteFont.setFontHeightInPoints((short)20);
contentWriteCellStyle.setWriteFont(contentWriteFont); contentWriteCellStyle.setWriteFont(contentWriteFont);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现 // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy = HorizontalCellStyleStrategy horizontalCellStyleStrategy =

1
update.md

@ -17,6 +17,7 @@
* `Converter`支持null转换 [Issue #1776](https://github.com/alibaba/easyexcel/issues/1776) * `Converter`支持null转换 [Issue #1776](https://github.com/alibaba/easyexcel/issues/1776)
* cglib 新增命名策略,防止和`spring`的冲突 [Issue #2064](https://github.com/alibaba/easyexcel/issues/2064) * cglib 新增命名策略,防止和`spring`的冲突 [Issue #2064](https://github.com/alibaba/easyexcel/issues/2064)
* 修改填充可能填充错误的bug [Issue #2035](https://github.com/alibaba/easyexcel/issues/2035) * 修改填充可能填充错误的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 # 2.2.10
* 修复读取的时候用string接收数字 可能四舍五入不一致的bug * 修复读取的时候用string接收数字 可能四舍五入不一致的bug

Loading…
Cancel
Save