alibaba / easyexcel

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

是不是不支持 lombok 的 @Accessors(chain = true) 注解 #3905

Closed WT-AHA closed 3 months ago

WT-AHA commented 3 months ago

建议先去看文档

快速开始常见问题

触发场景描述

当用于读取excel的实体类使用了 @Accessors(chain = true) 注解时,属性就会映射不上,使用 @Data 注解是正常的,看下后续是否准备支持此注解

image

触发Bug的代码

   package com.aha.common.open.source.easyexcel;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 复现 easyExcel 的问题
 */
@Slf4j
@RestController
@RequestMapping("/easy/excel")
public class TestEasyExcelBug {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 将制度规章导入到知识库中
     */
    @PostMapping("/file")
    public String importToRegulation (MultipartFile file) {

        try {

            EasyExcelFactory
                    .read(file.getInputStream(), User.class, new UserListener())
                    .sheet().doRead();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return "success";

    }

    @Data
    @Accessors(chain = true)
    public static class User {

        @ExcelProperty(index = 0)
        private String name;
        @ExcelProperty(index = 1)
        private Integer age;

    }

    @Slf4j
    public static class UserListener extends AnalysisEventListener<User> {

        @Override
        public void onException(Exception exception, AnalysisContext context) throws Exception {
            super.onException(exception, context);
        }

        /**
         * 这个每一条数据解析都会来调用
         */
        @Override
        public void invoke(User data, AnalysisContext context) {

            try {
                log.info("解析到一条数据:{}", objectMapper.writeValueAsString(data));
            } catch (JsonProcessingException e) {
                log.error("序列化成 json 失败: {}", e.getMessage());
            }

        }

        @Override
        public void extra(CellExtra extra, AnalysisContext context) {
            super.extra(extra, context);
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

        @Override
        public boolean hasNext(AnalysisContext context) {
            return super.hasNext(context);
        }

    }

}

提示的异常或者没有达到的效果

大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。 至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。

AutomaticCreation commented 3 months ago

我今天也遇到同样的问题,当我在导入导出模板类上添加@Accessors(chain= true)时,进行数据导入时,属性字段映射数据总是为null,当我去掉此注解时,映射成功.我当前的easyExcel的jar版本是:4.0.1

psxjoy commented 3 months ago

I think setter methods should focus on the single responsibility of setting properties. We don’t have plans to support this feature at the moment.

我认为 setter 方法应该专注于设置属性的单一职责。 目前暂时没有支持该特性的打算。

Elliotoplit commented 2 months ago

meet the same problem,so it will not be support in the future right>>?

psxjoy commented 2 months ago

meet the same problem,so it will not be support in the future right>>?

We don't have plans to support this feature at the moment. However, we may consider adding compatibility for it in future updates.