Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(read): 修复读取Map数据时最后一列数据为空时结果集中没有空列数据的问题 #3685

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(read): 修复读取Map数据时最后一列数据为空时结果及中没有空列数据的问题
修复当读取Map格式数据时,如果最后一列没有数据,那么读取到的map数据中不包含最后一列空数据的问题

Closes #3515
  • Loading branch information
nukiyoam committed Jan 31, 2024
commit f2260757d5df0ed921f7193802fe71397bbb7ae1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class ReadSheetHolder extends AbstractReadHolder {
private ReadCellData<?> tempCellData;
/**
* Read the size of the largest head in sheet head data.
* see https://github.com/alibaba/easyexcel/issues/2014
* @see <a href="https://github.com/alibaba/easyexcel/issues/2014">https://github.com/alibaba/easyexcel/issues/2014</a>
*/
private Integer maxNotEmptyDataHeadSize;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alibaba.excel.read.processor;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -91,7 +92,7 @@ private void onException(AnalysisContext analysisContext, Exception e) {

private void dealData(AnalysisContext analysisContext) {
ReadRowHolder readRowHolder = analysisContext.readRowHolder();
Map<Integer, ReadCellData<?>> cellDataMap = (Map)readRowHolder.getCellMap();
Map<Integer, ReadCellData<?>> cellDataMap = (Map) readRowHolder.getCellMap();
readRowHolder.setCurrentRowAnalysisResult(cellDataMap);
int rowIndex = readRowHolder.getRowIndex();
int currentHeadRowNumber = analysisContext.readSheetHolder().getHeadRowNumber();
Expand Down Expand Up @@ -124,9 +125,13 @@ private void buildHead(AnalysisContext analysisContext, Map<Integer, ReadCellDat
// Rule out empty head, and then take the largest column
if (MapUtils.isNotEmpty(cellDataMap)) {
cellDataMap.entrySet()
.stream()
.filter(entry -> CellDataTypeEnum.EMPTY != entry.getValue().getType())
.forEach(entry -> analysisContext.readSheetHolder().setMaxNotEmptyDataHeadSize(entry.getKey()));
.stream()
.filter(entry -> CellDataTypeEnum.EMPTY != entry.getValue().getType())
// 这里的size应该要 +1,不然在 com.alibaba.excel.read.listener.ModelBuildEventListener#buildNoModel() 里面判断head size和数据index的时候会有问题,导致有头但是空cell的数据不会被设置
// com/alibaba/excel/read/listener/ModelBuildEventListener.java:80
// fix https://github.com/alibaba/easyexcel/issues/3515
// fix https://github.com/alibaba/easyexcel/issues/2014
.forEach(entry -> analysisContext.readSheetHolder().setMaxNotEmptyDataHeadSize(entry.getKey() + 1));
}

if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.alibaba.easyexcel.test.temp.issue2014_3515;

import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* issue 3515 修复单元测试
*
* @see <a href="https://github.com/alibaba/easyexcel/issues/3515">https://github.com/alibaba/easyexcel/issues/3515</a>
* @author nukiyoam
*/
@Slf4j
public class Issue3515Test {

@Test
public void readWithHeadRowNumber(){
String fileName = String.join(File.separator, TestFileUtil.getPath(), "temp", "issue_3515", "issue_3515.xlsx");
List<Map<Integer, Object>> data = EasyExcel.read(fileName)
.sheet(0)
.headRowNumber(1)
.doReadSync();
Assertions.assertEquals(3, data.size());
data.forEach(it->{
Assertions.assertEquals(3,it.size());
});
}

@Test
public void readWithHeadList(){
String fileName = String.join(File.separator, TestFileUtil.getPath(), "temp", "issue_3515", "issue_3515.xlsx");
List<List<String>> head = new ArrayList<>();
head.add(Collections.singletonList("第一列"));
head.add(Collections.singletonList("第二列"));
head.add(Collections.singletonList("空列"));
List<Map<Integer, Object>> data = EasyExcel.read(fileName)
.sheet(0)
.head(head)
.doReadSync();
Assertions.assertEquals(3, data.size());
data.forEach(it->{
Assertions.assertEquals(3,it.size());
});
}
}
Binary file not shown.