How to do cell iteration of excel in java

前端 未结 3 1940
情书的邮戳
情书的邮戳 2020-12-10 22:44

I am having an excel with 2 rows and 5 columns. Now I entered code manually to take values from 1st row. How can I iterate the process?

Below is the code for 1st row

3条回答
  •  星月不相逢
    2020-12-10 23:17

    Use the code below to iterate over all rows of a datasheet:

    Sheet sheet = workbook.getSheet("Sheet1");
    for (Row row : sheet) {
        for (Cell cell : row) {
            //your logic
        }
    }
    

    Or, alternatively, use the following code:

    Sheet sheet = workbook.getSheet("Sheet1");
    for (int i = 0; i < 2; i++) {
        Row row = sheet.getRow(i);
        if(row == null) {
            //do something with an empty row
            continue;
        }
        for (int j = 0; j < 5; j++) {
            Cell cell = row.getCell(j);
            if(cell == null) {
                //do something with an empty cell
                continue;
            }
            //your logic
        }
    }
    

提交回复
热议问题