easyexcel 读excel场景,当headRowNumber大于1时,建议增加指定表头行的功能

a2mppw5e  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(43)

场景和问题

存在以下格式的excel,第一行是标题,第二行是填写说明,第三行往后是数据
| 字符串标题 | 日期标题 | 数字标题 |
| ------------ | ------------ | ------------ |
| 10个字以内 | yyyy/m/d H:mm格式 | 1 |
| 字符串0 | 2020/1/1 1:01 | 1 |
| 字符串1 | 2020/1/2 1:01 | 2 |
| 字符串2 | 2020/1/3 1:01 | 3 |

问题

设置headRowNumber为2,读取到的数据属性都为null

数据对象:

public class MultiHeadData {
    @ExcelProperty("字符串标题")
    private String string;
    @ExcelProperty("日期标题")
    private Date date;
    @ExcelProperty("数字标题")
    private Double doubleData;
}

读取excel

EasyExcel.read(fileName, MultiHeadData.class, new MultiHeadDataListener()).sheet().headRowNumber(2).doRead();

问题原因

下面代码判断只有在headRowNumber == rowIndex + 1的时候才会buildHead

// com.alibaba.excel.read.processor.DefaultAnalysisEventProcessor
private void dealData(AnalysisContext analysisContext) {
        ReadRowHolder readRowHolder = analysisContext.readRowHolder();
        Map<Integer, ReadCellData<?>> cellDataMap = (Map)readRowHolder.getCellMap();
        readRowHolder.setCurrentRowAnalysisResult(cellDataMap);
        int rowIndex = readRowHolder.getRowIndex();
        int currentHeadRowNumber = analysisContext.readSheetHolder().getHeadRowNumber();

        boolean isData = rowIndex >= currentHeadRowNumber;

        // Last head column
        if (!isData && currentHeadRowNumber == rowIndex + 1) {
            buildHead(analysisContext, cellDataMap);
        }
        // Now is data
        for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
            try {
                if (isData) {
                    readListener.invoke(readRowHolder.getCurrentRowAnalysisResult(), analysisContext);
                } else {
                    readListener.invokeHead(cellDataMap, analysisContext);
                }
            } catch (Exception e) {
                onException(analysisContext, e);
                break;
            }
            if (!readListener.hasNext(analysisContext)) {
                throw new ExcelAnalysisStopException();
            }
        }
    }

建议和讨论

增加headRowIndex属性,指定使用哪行做为真正的表头,我在本地修改了这个问题,如果方案可行的话,我愿意贡献源码。

lymgl2op

lymgl2op1#

public class MultiHeadData {
    @ExcelProperty("字符串标题")
    private String string;
    @ExcelProperty("日期标题")
    private Date date;
    @ExcelProperty("数字标题")
    private Double doubleData;
}
读取excel

EasyExcel.read(fileName, MultiHeadData.class, new MultiHeadDataListener()).sheet().headRowNumber(2).doRead();
问题原因
下面代码判断只有在headRowNumber == rowIndex + 1的时候才会buildHead

// com.alibaba.excel.read.processor.DefaultAnalysisEventProcessor
private void dealData(AnalysisContext analysisContext) {
        ReadRowHolder readRowHolder = analysisContext.readRowHolder();
        Map<Integer, ReadCellData<?>> cellDataMap = (Map)readRowHolder.getCellMap();
        readRowHolder.setCurrentRowAnalysisResult(cellDataMap);
        int rowIndex = readRowHolder.getRowIndex();
        int currentHeadRowNumber = analysisContext.readSheetHolder().getHeadRowNumber();

        boolean isData = rowIndex >= currentHeadRowNumber;

        // Last head column
        if (!isData && currentHeadRowNumber == rowIndex + 1) {
            buildHead(analysisContext, cellDataMap);
        }
        // Now is data
        for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
            try {
                if (isData) {
                    readListener.invoke(readRowHolder.getCurrentRowAnalysisResult(), analysisContext);
                } else {
                    readListener.invokeHead(cellDataMap, analysisContext);
                }
            } catch (Exception e) {
                onException(analysisContext, e);
                break;
            }
            if (!readListener.hasNext(analysisContext)) {
                throw new ExcelAnalysisStopException();
            }
        }
    }

相关问题