javaexcelapache-poipoi-hssf

How to get row count of a particular column in an excel file using poi library


I just wanted to know the numbers of rows filled with content with specific columns. I have a excel file in which column(1) contain 10 filled rows and column(3) contain 5 filled rows. By running below code it gives me total no of rows that are filled which is 10 but i just wanted to know the result of column(3) which has 5 rows filled. Just like changing the row sheet.getRow(2).getPhysicalNumberOfCells() give me the result of filled columns on any seperate rows? how will i get the result of a filled rows on a seperate columns?

    FileInputStream fis = new FileInputStream("D:\\File.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(fis);
    XSSFSheet sheet = workbook.getSheet("Sample");
    Qualification qual = new Qualification();

    int noOfColumns = sheet.getRow(2).getPhysicalNumberOfCells();
    int noOfRows = sheet.getPhysicalNumberOfRows();

    System.out.println("No of Rows: " + noOfRows);
    System.out.println("No of Columns: " + noOfColumns);

No of Rows: 10

No of Columns: 2


Solution

  • There is no built in function to do this but you can write it yourself like so:

    int count = 0;
    int columnNum = 3;
    for (Row row : sheet) {
        if (row.getCell(columnNum) != null) {
            count += 1;
        }
    }