eec icon indicating copy to clipboard operation
eec copied to clipboard

【求助】支持获取行号吗?

Open xiaoxpai opened this issue 11 months ago • 4 comments

起因;导入的时候难免做数据校验,如果数据重复或者不符合业务要求,需要给前端友好的提示,那么行号必不可少。

xiaoxpai avatar Mar 17 '25 07:03 xiaoxpai

可以的,注解使用@RowNum获取,或者使用row.getRowNum()方法获取,获取的行号为打开Excel左侧看到的行号从1开始

wangguanquan avatar Mar 17 '25 09:03 wangguanquan

  • 支持在解析过程中,用监听的方式,实现解析每一行,有错误可以定位到哪一行数据

  • 尝试使用row.getRowNum()方法获取,返回的数据都是-1

public class SpecItems extends Row{
...
}

解析excel

 List<SpecItems > dataList = reader.sheets()
                    .peek(sheet-> sheet.header(1))
                    .flatMap(Sheet::rows)
                    .map(row -> row.to(SpecItems.class))
                     .peek(r-> log.info("解析数据:{}", JSON.toJSONString(r)))
                    .collect(Collectors.toList());

我尝试使用 row.getRowNum() 获取的行号都是 -1

解析的数据
解析数据:{"blank":true,"s1":"hh","s2":"三","s3":"234","empty":true,"firstColumnIndex":0,"hidden":false,"lastColumnIndex":-1,"rowNum":-1,}
解析数据:{"blank":true,"s1":"hh","s2":"三","s3":"235","empty":true,"firstColumnIndex":0,"hidden":false,"lastColumnIndex":-1,"rowNum":-1,}
解析数据:{"blank":true,"s1":"hh","s2":"三","s3":"236","empty":true,"firstColumnIndex":0,"hidden":false,"lastColumnIndex":-1,"rowNum":-1,}

xiaoxpai avatar Mar 19 '25 03:03 xiaoxpai

SpecItems不需要继承Row,它是一个普通对象

public static class SpecItems {
    @ExcelColumn("商品编码")
    private String goodsNo;
    @RowNum
    private int rowNum; // 注入行号
}

第1行为表头的话可以简单使用dataRows List<SpecItems> list = reader.sheets().flatMap(Sheet::dataRows).map(row -> row.to(SpecItems.class)).collect(Collectors.toList());

Row在EEC里是一个内在共享的对象,如果使用row.getRowNum()取行号则不能在收集后获取

List<Map<String, Object>> list = reader.sheets().flatMap(Sheet::rows).map(row -> {
    Map<String, Object> map = row.toMap();
    map.put("rowNum", row.getRowNum());
    return map;
}).collect(Collectors.toList());

wangguanquan avatar Mar 19 '25 05:03 wangguanquan

现在使用注解的方式,已经可以拿到行号了👍🙏


解析数据:{"blank":true,"s1":"hh","s2":"三","s3":"234","empty":true,"firstColumnIndex":0,"hidden":false,"lastColumnIndex":-1,"rowNum":2,}
解析数据:{"blank":true,"s1":"hh","s2":"三","s3":"235","empty":true,"firstColumnIndex":0,"hidden":false,"lastColumnIndex":-1,"rowNum":3,}
解析数据:{"blank":true,"s1":"hh","s2":"三","s3":"236","empty":true,"firstColumnIndex":0,"hidden":false,"lastColumnIndex":-1,"rowNum":4,}

xiaoxpai avatar Mar 19 '25 08:03 xiaoxpai