Siva Kumar
Siva Kumar

Reputation: 1

Java Apache POI reading empty column after row data column

I have 100 rows 10 columns. reading each row one by one and column also. After 10th column the cursor not moving into next row. It's going to 11 12 13 etc column. Could anyone help me how to move the next row once reading 10th column and how to stop reading empty 11 column.

Here is some code:

while (rowIterator.hasNext()) { 
    row = rowIterator.next(); 
    while (cellIterator.hasNext()) { 
        cell = cellIterator.next(); 
        if(cell.getColumnIndex()==0 ) { } 
        ..... 
        if(cell.getColumnIndex()==10) { } 
    }
}

Upvotes: 0

Views: 826

Answers (2)

DevMateus
DevMateus

Reputation: 301

if you want to skip column you can use

while (cellsInRow.hasNext()) {
     Cell currentCell = cellsInRow.next();

     if (currentCell.getColumnIndex() >= 3 && currentCell.getColumnIndex() <= 6) {
         continue;
     }
         }

to skip column 4, 5, 6 and 7 from excel file. index starts at 0

Upvotes: 0

jmarkmurphy
jmarkmurphy

Reputation: 11473

First, but this will not necessarily fix your problem, you should be using the for each syntax to iterate rows and columns, then once you get past column 10 you can just break out of the loop like so:

for (Row row : sheet){
    for (Cell cell : row) {
        ...
        if (cell.getColumnIndex() >= 10) break;
    }
}

This is documented in the POI Quick Guide here: https://poi.apache.org/spreadsheet/quick-guide.html#Iterator

NOTE: I am breaking when column index is 10 or greater (that would be the 11th column as the indexes are 0 based). I mention this only because your code example is using column indexes 0 - 10, but your text says that there are only 10 valid columns.

Upvotes: 1

Related Questions