jueyue / easypoi

POI tool, excel quick import and export, excel template export, word template export, can complete the import and export of Excel in only 5 lines of code, the modification and export format is simple, rough, fast and effective, easypoi is worth your try
http://opensource.afterturn.cn
Apache License 2.0
143 stars 28 forks source link

复杂表头时列名映射错误 #27

Open aofall opened 2 years ago

aofall commented 2 years ago

如图所示表格,一对多的情况下,附加关联的 List 集合的部分映射没有问题,但普通列的第一列列名解析错误

QQ截图20220610152503

复现步骤:

一.定义关联类如下

1.关联类

@Data
public class DicPoi {
    @Excel(name = "* 字段名称", needMerge = true)
    private String name;

    @Excel(name = "* 字段编码", needMerge = true)
    private String code;

    @Excel(name = "* 字段类型", needMerge = true)
    private String type;

    @Excel(name = "* 数据长度", needMerge = true)
    private Long length;

    @ExcelCollection(name = "关联API")
    private List<DicApi> dicApiList;

    @ExcelCollection(name = "关联画像")
    private List<DicProfile> dicProfileList;
}

2.两个子关联集合类

@Data
public class DicApi {
    @Excel(name = "API名称")
    private String name;

    @Excel(name = "入参/出参")
    private String paramType;

    @Excel(name = "参数名称")
    private String paramName;

    @Excel(name = "参数code")
    private String paramCode;
}

@Data
public class DicProfile {
    @Excel(name = "画像名称")
    private String profileName;

    @Excel(name = "因子名称")
    private String paramName;

    @Excel(name = "因子编码")
    private String paramCode;
}

二.运行测试代码

public class Test {
    public static void main(String[] args) {
        String filePath = "/opt/导入示范模板.xlsx";
        ImportParams params = new ImportParams();
        params.setNeedVerfiy(true);
        params.setTitleRows(1);
        params.setHeadRows(2);
        ExcelImportResult<DicPoi> result = ExcelImportUtil.importExcelMore(new File(filePath), DicPoi.class, params);
        for (DicPoi dicPoi : result.getList()) {
            System.out.println(dicPoi);
        }
    }
}

三.运行

运行后,sout 输出内容

DicPoi(name=null, code=U0010001, type=字符型, length=8, dicApiList=[DicApi(name=身份信息查询, paramType=输出参数, paramName=姓名, paramCode=xm), DicApi(name=人员资质信息, paramType=输出参数, paramName=姓名, paramCode=name)], dicProfileList=[DicProfile(profileName=用户画像, paramName=姓名, paramCode=name), DicProfile(profileName=null, paramName=null, paramCode=null)])

可以看到关联的 name 属性读取为 null。遂打断点调试,运行至 cn.afterturn.easypoi.excel.imports.ExcelImportServer#importExcel 134行

Map<Integer, String> titlemap = this.getTitleMap(rows, params, excelCollection);

到此发现 titlemap 值不对,第一列标题被归纳到了上一级,与注解中 name 属性的列名映射不一致所以才会出现读取为 null 的情况,不知道是我的用法不对还是复杂表头映射的问题呢。

image