easypoi Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

在使用easypoi一对多导出功能时,报了这个错误。映射实体类代码如下:

@Data
public class PartnerDto{

    @ExcelIgnore
    private Long id;

    @Excel(name = "昵称",needMerge = true)
    private String name;

    @Excel(name = "邮箱",needMerge = true)
    private String email;
    
    @ExcelCollection(name = "关联公司")
    private List<String> companyList;

}

由于源码中有这么段代码

easypoi Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0_第1张图片
第144行的判断。导致PartnerDto类中字段companyList没有被easypoi所获取。导致 List list = new ArrayList(); 中的list 是个size为0的集合。
easypoi Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0_第2张图片
所有后面创建cell时会报异常。
easypoi Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0_第3张图片
正确的做法如下:

@Data
public class PartnerDto{

    @ExcelIgnore
    private Long id;

    @Excel(name = "昵称",needMerge = true)
    private String name;

    @Excel(name = "邮箱",needMerge = true)
    private String email;
    
    @ExcelCollection(name = "关联公司")
    private List<InnerCompany> companyList;


    public static class InnerCompany {

        public InnerCompany() {
        }

        public InnerCompany(String company) {
            this.company = company;
        }

        @Excel(name = "公司")
        private String company;


        public String getCompany() {
            return company;
        }

        public void setCompany(String company) {
            this.company = company;
        }
        
    }

}

再进行excel导出时就不会报错了。

你可能感兴趣的:(Exception,java)