简体   繁体   English

如何使用Apache POI停止Java中的迭代

[英]How do I stop iteration in Java using Apache POI

I am reading a particular excel template in Java using Apache POI. 我正在使用Apache POI阅读Java中的特定excel模板。 There are some unwanted fields at the bottom (like a small guide) which I need to skip. 底部有一些不需要的字段(例如小指南),我需要跳过。 So I want to stop the iteration whenever POI finds that a row is completely blank and hence the fields at the bottom will be skipped automatically. 所以我想在POI发现一行完全空白时停止迭代,因此底部的字段将被自动跳过。

I found many ways to handle a blank CELL but my requirement is for a complete row. 我发现了许多处理空白CELL的方法,但我的要求是完整行。

Following code will iterate through the cells of a row. 以下代码将遍历一行的单元格。 it will keep a count for each cell it encounters(cellCount). 它将为每个遇到的单元格保留一个计数(cellCount)。 another couont(nullCount), is incremented only if the cell is blank. 仅当单元格为空白时,另一个couont(nullCount)才增加。 if both these counts are equal, row is empty. 如果这两个计数相等,则行为空。

boolean isEmptyRow = false;
            int cellCount = 0;
            int nullCount = 0;

                Iterator<Cell> cellIterator = row.iterator();
                while (cellIterator.hasNext()) {
                    cellCount++;
                    Cell cell = (Cell) cellIterator.next();
                    if ((cell == null)
                            || ((cell != null) && (cell.getCellType() == Cell.CELL_TYPE_BLANK))) {
                        nullCount++;
                    }
                }

            if ((cellCount != 0) && (nullCount != 0)
                    && (cellCount == nullCount)) {        //If nullCount & cellCouont are same, Row is empty
                isEmptyRow = true;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM