如何做好小区迭代EXCEL在java的(How to do cell iteration of ex

2019-09-01 03:42发布

我有2行5列的Excel。 现在我手动输入代码从第1行取值。 我怎样才能迭代过程?

下面是在Excel中第一行代码。 从第二排,我不知道该怎么办......我要陆续迭代一行。

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();

Answer 1:

使用下面的代码来遍历一个数据表中的所有行:

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

或者,使用下面的代码:

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
    }
}


Answer 2:

你只需要改变1到行号,并使用一个for循环。 基于您的代码,你可以做以下例如对于〔实施例:

for(int i=0; i<number; i++)
{

            Cell a = sheet.getCell(2,i);
            Cell b = sheet.getCell(3,i);
            Cell c = sheet.getCell(4,i);
...

}

或者更好的是将单元格的声明出来的for循环,这是更好的实践。

Cell a,b, c...; 

for(int i=0; i<number; i++)
{

             a = sheet.getCell(2,i);
             b = sheet.getCell(3,i);
             c = sheet.getCell(4,i);
...

}


Answer 3:

for (int i = 0; i <= sheet.getLastRowNum (); ++i) {
        row=(Row) sheet.getRow(i);
        Iterator<Cell> cells = row.cellIterator();
        while (cells.hasNext())
        { .....

你可以使用上面的代码遍历所有行和细胞



文章来源: How to do cell iteration of excel in java