Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当类的属性没有按照ExcelProperty的属性index顺序排序的时候,写数据出现错乱 #1046

Closed
liuxuzxx opened this issue Jan 2, 2020 · 5 comments
Assignees
Labels
bug Something isn't working developing This feature will be added in future releases pending verification This problem needs to be confirmed

Comments

@liuxuzxx
Copy link
Contributor

liuxuzxx commented Jan 2, 2020

触发场景描述
当类的属性没有按照ExcelProperty注解的属性index排序的时候,写数据出现错乱顺序
触发Bug的代码

   public class EasyExcelFound {

    /**
     * 写数据
     */
    @Test
    public void indexNotSort() {
        Path path = Paths.get("/home/liuxu/Public/not-sort.xlsx");
        ExcelWriter excelWriter = EasyExcel.write(path.toFile())
                .build();
        WriteSheet writeSheet = EasyExcel.writerSheet("出现不排序的情况")
                .head(head())
                .build();

        excelWriter.write(data(),writeSheet);
        excelWriter.finish();
    }

    private List<List<String>> head() {
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("名字"));
        head.add(Collections.singletonList("ID编号"));
        return head;
    }

    public List<CluesDto> data() {
        return IntStream.rangeClosed(0, 3)
                .boxed()
                .map(integer -> {
                    CluesDto cluesDto = new CluesDto();
                    cluesDto.setId(integer);
                    cluesDto.setName("线索名字-" + integer);
                    cluesDto.setCreateDate(new Date());
                    return cluesDto;
                })
                .collect(Collectors.toList());
    }
}

@ExcelIgnoreUnannotated
class CluesDto {
    @ExcelProperty(index = 1)
    private Integer id;

    @ExcelProperty(index = 0)
    private String name;

    private Date createDate;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

提示的异常或者没有达到的效果
写入结果为:
not-sort

可以看到excel中的:名字和ID编号两列写反了。

目前的解决方案:在CluesDto类中,把id,name属性按照index的增长顺序写就行,也就是调整为:

@ExcelIgnoreUnannotated
class CluesDto {
    @ExcelProperty(index = 0)
    private String name;

    @ExcelProperty(index = 1)
    private Integer id;

    private Date createDate;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

跟踪了下源码,发现:
com.alibaba.excel.util.ClassUtils类中获取declaredFields的方法,获取allFieldList没有按照ExcelProperty的index进行排序。导致后面写入数据的时候获取fieldList只是按照反射拿取的字段顺序为准,并没有按照index排序。

@liuxuzxx liuxuzxx added the bug Something isn't working label Jan 2, 2020
@liuxuzxx
Copy link
Contributor Author

liuxuzxx commented Jan 2, 2020

忘记了测试版本:2.1.3和2.1.4均会出现此问题(2.1.4,我是从maven中央仓库拉取的,不知道为啥github上没有2.1.4的发行版本)

@zhuangjiaju zhuangjiaju added the pending verification This problem needs to be confirmed label Jan 7, 2020
@zhuangjiaju zhuangjiaju added the developing This feature will be added in future releases label Mar 17, 2020
zhuangjiaju added a commit that referenced this issue Apr 22, 2020
修改issue #1046(当类的属性没有按照ExcelProperty的属性index顺序排序的时候,写数据出现错乱)
@zhuangjiaju
Copy link
Collaborator

已经合并

@ColoZhu
Copy link

ColoZhu commented May 9, 2020

有没有能直接定义列字段顺序的方式,出了注解比如"id"绑定顺序index=0?

@liuxuzxx
Copy link
Contributor Author

liuxuzxx commented May 9, 2020

有没有能直接定义列字段顺序的方式,出了注解比如"id"绑定顺序index=0?

这个直接定义列字段顺序是什么意思?使用ExcelProperty就可以直接定义列的顺序了

@ColoZhu
Copy link

ColoZhu commented Jun 8, 2020

我是说除了ExcelProperty这种注解方式,比如我excel要显示 "id","name","age" 这种顺序, 然后我定义excel列明"主键ID", " 姓名","年龄"; 我直接传对应的这两个String[] keysArr , String[] namesArr ,这样参数, 然后能按照我这种格式来输出Excel中~ 这样我们输出excle时候会更灵活~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working developing This feature will be added in future releases pending verification This problem needs to be confirmed
Projects
None yet
Development

No branches or pull requests

3 participants