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

使用hasNext方法提前退出excel的解析后, 没有执行 doAfterAllAnalysed 方法 #3159

Closed
yixiu025 opened this issue May 3, 2023 · 4 comments
Labels
help wanted Extra attention is needed planning It may be developed later

Comments

@yixiu025
Copy link

yixiu025 commented May 3, 2023

建议先去看文档

快速开始常见问题

异常代码

public class DemoReadListener extends AnalysisEventListener<Map<Integer, String>> {

    private List<Map<Integer, String>> dataList = new ArrayList<>();

    private boolean hasNext = true;

    private boolean processed = false;

    @Override
    public void invoke(Map<Integer, String> dataMap, AnalysisContext analysisContext) {
        if(!StringUtils.hasText(dataMap.get(1))) {
            hasNext = false;
            return;
        }

        dataList.add(dataMap);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 模拟处理剩余的一批数据
        System.out.println("解析到的数据条数: " + dataList.size());
        processed = true;
    }

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

    public boolean isProcessed() {
        return processed;
    }

}
@Test
void normallyQuitTest() {
    // 没有提前退出读取, 执行了 doAfterAllAnalysed 在内部完成了对数据的操作
    DemoReadListener normallyQuitReadListener = new DemoReadListener();
    EasyExcel.read(this.getClass().getResourceAsStream("/测试导入文件-结尾无数据.xlsx"))
            .excelType(ExcelTypeEnum.XLSX)
            .registerReadListener(normallyQuitReadListener)
            .headRowNumber(1)
            .doReadAll();
    Assertions.assertTrue(normallyQuitReadListener.isProcessed());
}

@Test
void earlyQuitTest() {
    // 提前退出读取, 没执行 doAfterAllAnalysed 没有在内部完成对数据的操作
    DemoReadListener earlyQuitReadListener = new DemoReadListener();
    EasyExcel.read(this.getClass().getResourceAsStream("/测试导入文件-结尾有数据.xlsx"))
            .excelType(ExcelTypeEnum.XLSX)
            .registerReadListener(earlyQuitReadListener)
            .headRowNumber(1)
            .doReadAll();
    Assertions.assertTrue(earlyQuitReadListener.isProcessed());
}

异常提示

问题描述

需求:

客户方提供的excel文档, [可能在数据行的下方存在参考档案的描述或其他信息,我需要读取其中有效的数据

问题点:

根据特定的判定数据行结束的逻辑结合hasNext方法实现了读取的提前退出, 但是如果数据行下方存在其他的参考档案时, 会导致 doAfterAllAnalysed 方法没有执行。

环境:

JDK1.8, easyexcel3.2.1, springboot2.6.13

@yixiu025 yixiu025 added the help wanted Extra attention is needed label May 3, 2023
@yixiu025
Copy link
Author

yixiu025 commented May 3, 2023

参看此仓库代码

@gongxuanzhang
Copy link
Collaborator

感谢这么详细的描述,目前项目里的判断逻辑设计是:

当用户手动停止时,不会触发doAfterAllAnalysed() 回调函数

目前你的需求建议把执行逻辑放在DemoReadListener中, 可以在invoke()的代码最开始加一个卫语句

 if(!hasNext){
 return;
}

这应该是一个设计缺陷,我们会调整一下用户手动终止的逻辑,感谢你的issue

@yixiu025
Copy link
Author

yixiu025 commented May 4, 2023

感谢这么详细的描述,目前项目里的判断逻辑设计是:

当用户手动停止时,不会触发doAfterAllAnalysed() 回调函数

目前你的需求建议把执行逻辑放在DemoReadListener中, 可以在invoke()的代码最开始加一个卫语句

 if(!hasNext){
 return;
}

这应该是一个设计缺陷,我们会调整一下用户手动终止的逻辑,感谢你的issue

感谢你的答复,我现在实际项目中是按照上述方案处理的。

@gongxuanzhang gongxuanzhang added the planning It may be developed later label May 4, 2023
@gongxuanzhang
Copy link
Collaborator

gongxuanzhang commented Jul 19, 2024

你好,我们发现此Issue已经超过三个月没有活动了,为了更好的帮助您解决问题,我们将在两周后关闭此Issue,如果您仍然有问题,请在两周内内回复此Issue,谢谢!如果您已经解决或者不需要帮助,请忽略此消息。

@gongxuanzhang gongxuanzhang added the expire-candidate If don't respond within seven days, will be closed label Jul 19, 2024
@gongxuanzhang gongxuanzhang removed the expire-candidate If don't respond within seven days, will be closed label Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed planning It may be developed later
Projects
None yet
Development

No branches or pull requests

2 participants