Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
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
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.