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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16215750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:50:03  来源:igfitidea点击:

How to do cell iteration of excel in java

javaapache-poi

提问by user2314399

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?

我有一个 2 行 5 列的 excel。现在我手动输入代码以从第一行获取值。我怎样才能迭代这个过程?

Below is the code for 1st row in excel. From the 2nd row on, I dont know how to do... I want to iterate one row after another.

下面是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();

回答by skuntsel

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

回答by Menios

You just need to change 1 to the row number, and use a for loop. Based on your code you could do the following e.g. For exmaple:

您只需要将 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);
...

}

Or better yet move the Cell declarations out of the for loop which is better practice.

或者更好的是将 Cell 声明移出 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);
...

}

回答by Prashant Gautam

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

you can use the above code to iterate all rows and all cells

您可以使用上面的代码迭代所有行和所有单元格