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

读取多sheet文件时,只获取了第一个sheet 版本:3.2.1 #3930

Closed
vnsun opened this issue Aug 13, 2024 · 10 comments
Closed

读取多sheet文件时,只获取了第一个sheet 版本:3.2.1 #3930

vnsun opened this issue Aug 13, 2024 · 10 comments
Assignees
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@vnsun
Copy link

vnsun commented Aug 13, 2024

读取多个sheet文件时,只能获取第一个sheet数据
以下是读取代码,第二个监听器方法没有回调执行
image
下面是我的文件,几经确认sheet名称无错。且指定下标执行结果依旧读取不到第二个sheet
image

debug
image
image

只读取一个指定的sheet正常获取数据
版本3.2.1 升级版本3.3.4、4.0.1 依然没有解决此问题

@vnsun vnsun added the bug Something isn't working label Aug 13, 2024
@vnsun
Copy link
Author

vnsun commented Aug 13, 2024

image
指定读取第二个sheet情况,由于本地代码库拉不下来,导致定位debug混乱 没继续往下了。望解答此问题

@psxjoy
Copy link
Collaborator

psxjoy commented Aug 13, 2024

Please provide a minimal reproducible demo.
麻烦提供最小复现代码和对应的文件。

@psxjoy psxjoy added help wanted Extra attention is needed question Further information is requested and removed bug Something isn't working labels Aug 15, 2024
@vnsun
Copy link
Author

vnsun commented Aug 20, 2024

复现代码

  1. 执行
String path = "C:\\Users\\vn_su\\Desktop\\acc.xlsx";
        File file = new File(path);
        FileInputStream inputStream = new FileInputStream(file);
        ExcelReader reader = EasyExcel.read(inputStream).build();

        CreativeQuotationAccFeeTmpDeliverablesListener deliverablesListener = new CreativeQuotationAccFeeTmpDeliverablesListener(1L);
        ReadSheet sh1 = EasyExcel.readSheet("By Deliverables martix").head(CvQuotationAccFeeTmpDeliverablesExcel.class)
                .headRowNumber(3).registerReadListener(deliverablesListener).build();
        CreativeQuotationAccFeeTmpMediaInnoListener mediaInnoListener = new CreativeQuotationAccFeeTmpMediaInnoListener(1L);
        ReadSheet sh2 = EasyExcel.readSheet("Media inno OLB").head(CvQuotationAccFeeTmpMediaInnoExcel.class)
                .headRowNumber(3).registerReadListener(mediaInnoListener).build();
        reader.read(sh1, sh2);
        List<CvQuotationAccFeeTmpDeliverablesExcel> deliverablesList = deliverablesListener.list;
        List<CvQuotationAccFeeTmpMediaInnoExcel> mediaInnoList = mediaInnoListener.list;
  1. 监听
@Slf4j
public class CreativeQuotationAccFeeTmpDeliverablesListener extends AnalysisEventListener<CvQuotationAccFeeTmpDeliverablesExcel> {

    public final List<CvQuotationAccFeeTmpDeliverablesExcel> list = new ArrayList<>();
    public BigDecimal totalCost = BigDecimal.ZERO;
    private CvQuotationAccFeeTmpDeliverablesExcel prev;
    private boolean nextBreakFlag = true;
    private final Long naId;

    public CreativeQuotationAccFeeTmpDeliverablesListener(Long naId) {
        this.naId = naId;
    }

    @Override
    public void invoke(CvQuotationAccFeeTmpDeliverablesExcel cvQuotationAccFeeTmpDeliverablesExcel, AnalysisContext analysisContext) {
        String deliverables = cvQuotationAccFeeTmpDeliverablesExcel.getDeliverables();
        // 中断
        if (StrUtil.isBlank(deliverables)) {
            nextBreakFlag = false;
            totalCost = cvQuotationAccFeeTmpDeliverablesExcel.getCost() == null ? BigDecimal.ZERO : cvQuotationAccFeeTmpDeliverablesExcel.getCost();
            return;
        }
        processNull(prev, cvQuotationAccFeeTmpDeliverablesExcel);
        cvQuotationAccFeeTmpDeliverablesExcel.setCostMergeAnnex(String.valueOf(cvQuotationAccFeeTmpDeliverablesExcel.getCost()));
        // 判断是否合并
        if (prev != null && prev.getProjectTypeAnnex().equals(cvQuotationAccFeeTmpDeliverablesExcel.getProjectTypeAnnex())
            && cvQuotationAccFeeTmpDeliverablesExcel.getCost().compareTo(BigDecimal.ZERO) == 0) {
            cvQuotationAccFeeTmpDeliverablesExcel.setCostMergeAnnex(StrUtil.DASHED);
        }
        prev = cvQuotationAccFeeTmpDeliverablesExcel;
        list.add(cvQuotationAccFeeTmpDeliverablesExcel);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("CvQuotationAccFeeTmpDeliverablesExcel解析完毕");
    }

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

    /**
     * 处理空值
     * @param prev 上一个实例
     * @param current 当前实例
     */
    private void processNull(CvQuotationAccFeeTmpDeliverablesExcel prev, CvQuotationAccFeeTmpDeliverablesExcel current) {
        if (prev == null) {
            return;
        }
        if (StrUtil.isBlank(current.getNumber())) {
            current.setNumber(prev.getNumber());
        }
    }

}
@Slf4j
public class CreativeQuotationAccFeeTmpMediaInnoListener extends AnalysisEventListener<CvQuotationAccFeeTmpMediaInnoExcel> {

    public final List<CvQuotationAccFeeTmpMediaInnoExcel> list = new ArrayList<>();
    public BigDecimal totalCost = BigDecimal.ZERO;
    private boolean nextBreakFlag = true;
    private final Long naId;

    public CreativeQuotationAccFeeTmpMediaInnoListener(Long naId) {
        this.naId = naId;
    }

    @Override
    public void invoke(CvQuotationAccFeeTmpMediaInnoExcel cvQuotationAccFeeTmpMediaInnoExcel, AnalysisContext analysisContext) {
        String projectType = cvQuotationAccFeeTmpMediaInnoExcel.getProjectType();
        // 中断
        if (StrUtil.isBlank(projectType)) {
            nextBreakFlag = false;
            totalCost = cvQuotationAccFeeTmpMediaInnoExcel.getCost() == null ? BigDecimal.ZERO : cvQuotationAccFeeTmpMediaInnoExcel.getCost();
            return;
        }
        list.add(cvQuotationAccFeeTmpMediaInnoExcel);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("CreativeQuotationAccFeeTmpMediaInnoListener解析完毕");
    }

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

}
  1. 实体
@Data
public class CvQuotationAccFeeTmpDeliverablesExcel {

    @ExcelIgnore
    private Long creativeNaId;
    @ExcelProperty(index = 0)
    private String number;
    @ExcelProperty(index = 1)
    private String projectType;
    @ExcelProperty(index = 2)
    private String projectTypeAnnex;
    @ExcelProperty(index = 3)
    private String deliverables;
    @ExcelProperty(index = 4)
    private String deliverablesMilestone;
    @ExcelProperty(index = 5)
    private BigDecimal cost;
    @ExcelProperty(index = 6)
    private String remark;
    @ExcelIgnore
    private String costMergeAnnex;
    @ExcelIgnore
    private Integer sort;

}


@Data
public class CvQuotationAccFeeTmpMediaInnoExcel {

    @ExcelIgnore
    private Long creativeNaId;
    @ExcelProperty("No")
    private String number;
    @ExcelProperty("Project Type")
    private String projectType;
    @ExcelProperty("Deliverables")
    private String deliverables;
    @ExcelProperty("Cost (w/o tax)")
    private BigDecimal cost;
    @ExcelProperty("Remark")
    private String remark;
    @ExcelIgnore
    private Integer sort;

}
  1. 文件
    acc.xlsx

@vnsun
Copy link
Author

vnsun commented Aug 20, 2024

另外还发现一个问题 代码同上
读取额外信息时 读不到(批注、合并单元格信息)

以下是不同代码

EasyExcel.read(inputStream)
                    .extraRead(CellExtraTypeEnum.COMMENT).sheet("By Deliverables martix").head(CvQuotationAccFeeTmpDeliverablesExcel.class)
                    .headRowNumber(3).registerReadListener(deliverablesListener).build();

CreativeQuotationAccFeeTmpDeliverablesListener监听器重写此代码没有进行调用

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

@vnsun
Copy link
Author

vnsun commented Aug 22, 2024

@zhuangjiaju @psxjoy

@vnsun
Copy link
Author

vnsun commented Aug 26, 2024

没有人来解答 或者看下这个问题么? @zhuangjiaju @psxjoy @frankggyy @clevertension

@psxjoy
Copy link
Collaborator

psxjoy commented Aug 26, 2024

I couldn't reproduce the issue. It's recommended to remove the business code and check step by step.
无法复现。建议剔除业务代码,一步步排查。

image

@vnsun
Copy link
Author

vnsun commented Aug 26, 2024

@psxjoy 我大概知道原因了 处理两个sheet的Listener类中重写了hasNext方法,第一个 处理程序执行完重写的hasNext方法返回false。影响到了第二个处理类没有执行。这应该算是bug。
注释掉重写的hasNext 执行正常
image

@psxjoy
Copy link
Collaborator

psxjoy commented Aug 26, 2024 via email

@vnsun
Copy link
Author

vnsun commented Aug 26, 2024

OK,很期待

@vnsun vnsun closed this as completed Aug 26, 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 question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants