alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.09k stars 7.5k forks source link

ExcelProperty 使用中value 值无法指定多个 #3889

Closed smallwatermelonx closed 1 month ago

smallwatermelonx commented 2 months ago

读取时值为null


// 这是实体类
public class BaseFloodREntity {
        @Basic
        @Column(name = "TM")
        @ExcelProperty(value = "TM", converter = CustomStringTimestampConverter.class)
        private Timestamp tm;

        @Basic
        @ExcelProperty(value = "DATA_ID")
        @Column(name = "DATA_ID")
        private String dataId;

        @ExcelProperty(value = {"值", "V"})
        @Basic
        @Column(name = "v")
        private BigDecimal v;

        @Basic
        @Column(name = "rscd")
        private String rscd;

        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Id
        @Column(name = "id")
        private int id;
    }
// 这是读取功能
 baseFloodREntities = EasyExcel.read(file.getInputStream())
                    .sheet()
                    // 自动检测标题行 跳过
                    .autoTrim(true)
                    .head(BaseFloodREntity.class)
                    .registerReadListener(new CustomListener(rscd))
                    .doReadSync();
// 这是自定义的listener 
public class CustomListener extends AnalysisEventListener<BaseFloodREntity> {

    // 处理excel中选定数据的rscd
    private final String rscd;

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        // 在这里处理异常情况,比如单元格为空
        int rowIndex = context.readRowHolder().getRowIndex();
        if (context.readRowHolder().getCellMap().containsValue(null)) {
            // 如果当前行有任何单元格为 null,则忽略该行
            log.warn("Row {} contains null value, skipping this row.", rowIndex);
            return;
        }
        // 其他异常情况的处理逻辑
    }

    public CustomListener(String rscd) {
        this.rscd = rscd;
    }

    @Override
    public void invoke(BaseFloodREntity baseFloodREntity, AnalysisContext context) {

        log.info("invoke: {}", baseFloodREntity);
        // 设置 rscd 字段
        baseFloodREntity.setRscd(rscd);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 其他后处理逻辑
        log.info("所有数据解析完成!");
    }
}
# 问题描述
在实体类中可以指定表头为大小写能读取,比如@ExcelProperty(value = {"v", "V"})。如果是@ExcelProperty(value = {"值", "V"})则无法读取到数据,在listner中的invoke阶段读的数据就为null。因为文档中没看到是否能够这样读取表头所以提交一个issue
, 谢谢大佬们帮忙解答.
psxjoy commented 2 months ago

参考注解中的注释

 /**
   * The name of the sheet header.
   *
   * <p>
   * write: It automatically merges when you have more than one head
   * <p>
   * read: When you have multiple heads, take the last one
   *
   * @return The name of the sheet header
   */
  String[] value() default {""};
psxjoy commented 1 month ago

I'm not sure if you've already resolved the issue. Let's go ahead and close this issue for now, but please feel free to reopen it if the problem continues.

smallwatermelonx commented 1 month ago

I'm not sure if you've already resolved the issue. Let's go ahead and close this issue for now, but please feel free to reopen it if the problem continues.

Thanks to you, I can solve my question already.