Luvs1015
Luvs1015

Reputation: 9

Last row of a column in excel using POI for selenium-java

I'm in a situation where I need to find the last row of a specific column. This is not the same as finding the last row of the sheet because it has to be a specific column. So I want to get the last row of column Header1 for clarity.

Hopefully, anyone already done this before and share it to me.

enter image description here

Upvotes: 0

Views: 1739

Answers (2)

Luvs1015
Luvs1015

Reputation: 9

As per suggestion of Alex above, I have done a loop to the specific column that I want to get the last row. Checking if its empty then the row before that will the last row.

This might not be perfect but it works for me.

Upvotes: 0

Zakaria Shahed
Zakaria Shahed

Reputation: 2697

    String SAMPLE_XLSX_FILE_PATH = "./GoogleAnalyticsevent.xlsx";
    // Creating a Workbook from an Excel file (.xls or .xlsx)
    Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
    // number of sheets in the Workbook
    System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
    // Sheet at index zero
    for(int i=1;i<workbook.getNumberOfSheets();i++) {
        Sheet sheet = workbook.getSheetAt(i);
        for (int colnum=0;colnum<sheet.getLastRowNum();colnum++){
            // Create a DataFormatter to format and get each cell's value as String
            // System.out.print(sheet.getRow(j).getCell(1) + "\n");

            // Closing the workbook
            workbook.close();
        }

    }

You can Change colnum and get your column & sheet.getLastRowNum() will retun you the last cell number

Upvotes: 1

Related Questions