0

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) { } 
    }
}
4
  • Reset the "cursor" to 1 after the end of a row. Commented Dec 22, 2016 at 7:12
  • 1
    Can you describe a bit more and include some code-snippets of what you are trying to do? Commented Dec 22, 2016 at 8:03
  • while (rowIterator.hasNext()) { row = rowIterator.next(); while (cellIterator.hasNext()) { cell = cellIterator.next(); if(cell.getColumnIndex()==0 ) { } ..... if(cell.getColumnIndex()==10) { } } In the above code am iterating each cell. after 10th cell I have empty columns. those empty columns are iterating But I want to move into next row after 10 cell and stop reading the empty cells like 11,12,13 etc. Commented Dec 22, 2016 at 9:01
  • I added a code snippet from the comments to your question. Commented Dec 22, 2016 at 13:53

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.