kemistrie
kemistrie

Reputation: 1

how to iterate correct over rows and cells in xlsx

i'm not sure, maybe we had an similar request here and i'm to stupid to find the right request.

My English is not so good, i hope i can explain what i need. I have the Task, to iterate through an xlsx File, which contains information about one Sprint and absence of our teamcollegues.

I need help for the iteration, i'm a noob and i try it with 'for loop' but it confused me totally.

Here is what i have now:

public void go() throws Exception, FileNotFoundException, IOException {
    FileInputStream fis = new FileInputStream(fileName);
    Workbook wb = WorkbookFactory.create(fis);
    Sheet sheet = wb.getSheetAt(25);

    Row ersteReihe = sheet.getRow(0);
    Cell datum1 = ersteReihe.getCell(1);  //Date in column B1


    System.out.println("\n\nAbwesenheit " +sheet.getSheetName() );

    // Hole aus der zweiten Reihe den ersten Namen:
    Row zweiteReihe = sheet.getRow(1);
    Cell name1 = zweiteReihe.getCell(0);     //Name of employee
    Cell isDa1 = zweiteReihe.getCell(1);     //Information with 1 and 0 who is absence
    int zell1 = (int) isDa1.getNumericCellValue();
    if (zell1 == 0) {
        System.out.print("\n" + name1.getStringCellValue() + " ist am " + datum1 + " nicht Anwesend ");
    }
    // Hole aus der zweiten Reihe den ersten Namen:
    Row dritteReihe = sheet.getRow(2);
    Cell name2 = dritteReihe.getCell(0);
    Cell isDa2 = dritteReihe.getCell(1);
    int zell2 = (int) isDa2.getNumericCellValue();
    if (zell2 == 0) {
        System.out.print("\n" + name2.getStringCellValue() + " ist am " + datum1 + " nicht Anwesend ");
    }
    // Hole aus der zweiten Reihe den ersten Namen:
    Row vierteReihe = sheet.getRow(3);
    Cell name3 = vierteReihe.getCell(0);
    Cell isDa3 = vierteReihe.getCell(1);
    int zell3 = (int) isDa3.getNumericCellValue();
    if (zell3 == 0) {
        System.out.print("\n" + name3.getStringCellValue() + " ist am " + datum1 + " nicht Anwesend ");
    }
    // Hole aus der zweiten Reihe den ersten Namen:
    Row fuenfteReihe = sheet.getRow(4);
    Cell name4 = fuenfteReihe.getCell(0);
    Cell isDa4 = fuenfteReihe.getCell(1);
    int zell4 = (int) isDa4.getNumericCellValue();
    if (zell4 == 0) {
        System.out.print("\n" + name4.getStringCellValue() + " ist am " + datum1 + " nicht Anwesend ");
    }
    // Hole aus der zweiten Reihe den ersten Namen:
    Row sechsteReihe = sheet.getRow(5);
    Cell name5 = sechsteReihe.getCell(0);
    Cell isDa5 = zweiteReihe.getCell(1);
    int zell5 = (int) isDa5.getNumericCellValue();
    if (zell5 == 0) {
        System.out.print("\n" + name5.getStringCellValue() + " ist am " + datum1 + " nicht Anwesend ");
    }
    // Hole aus der zweiten Reihe den ersten Namen:
    Row siebteReihe = sheet.getRow(6);
    Cell name6 = siebteReihe.getCell(0);
    Cell isDa6 = siebteReihe.getCell(1);
    int zell6 = (int) isDa6.getNumericCellValue();
    if (zell6 == 0) {
        System.out.print("\n" + name6.getStringCellValue() + " ist am " + datum1 + " nicht Anwesend ");
    }

    fis.close();

}

I going to be crazy. cause i'm not able to find the right solution for an correct iteration :(

Could someone help me please? Please be lenient with me :)

Thanks a lot! Cheers Adissa

Upvotes: 0

Views: 1436

Answers (1)

ZhekaKozlov
ZhekaKozlov

Reputation: 39586

Since Sheets and Rows are Iterables, iterating over rows and cells is very easy:

Sheet sheet = ...
for (Row row : sheet) {
    for (Cell cell : row) {
        // Actual work on cell here
    }
}

Upvotes: 2

Related Questions