如何在java中进行excel的单元迭代

use*_*399 2 java apache-poi

我有一个2行5列excel.现在我手动输入代码以从第1行获取值.我该如何迭代这个过程?

下面是excel第一行的代码.从第2行开始,我不知道该怎么做......我想迭代一行.

Workbook workbook = Workbook.getWorkbook(new File(
                               "\\C:\\users\\a-4935\\Desktop\\DataPool_CA.xls"));
Sheet sheet = workbook.getSheet("Sheet1");
System.out.println("Reached to Sheet");
Cell a = sheet.getCell(2,1);
Cell b = sheet.getCell(3,1);
Cell c = sheet.getCell(4,1);
Cell d = sheet.getCell(5,1);
Cell e = sheet.getCell(6,1);
Cell f = sheet.getCell(7,1);
Cell g = sheet.getCell(8,1);
Cell h = sheet.getCell(9,1);
Cell i = sheet.getCell(10,1);

String uId              =   a.getContents();
String deptFromDat      =   b.getContents();
String deptToDate       =   c.getContents();
String dept1            =   d.getContents();
String arrival1         =   e.getContents();
String eihon1           =   f.getContents();
String branchCode1      =   g.getContents();
String userType1        =   h.getContents();
String sessionId1       =   i.getContents();
Run Code Online (Sandbox Code Playgroud)

sku*_*sel 9

使用下面的代码迭代数据表的所有行:

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

或者,或者,使用以下代码:

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
    }
}
Run Code Online (Sandbox Code Playgroud)