Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
January 6, 2017

Row Count And Column Count in Excel





Row count and column count in excel will discuss how we can get the information about row count and column count of an excel while automating any application using selenium web driver. Usually we will use the excel sheet to maintain the test data. We might get doubt that why we need to put the test data/results in excel. If an application need so much of data to be feed to test; then we will manage those data using external sources like text file, xml file and excel file etc… . Among these; Excel is very user friendly that we can organize the data very easily and we can read and write the data how we want. So, we mostly use excel to maintain the test data and results. In order to create a data driven framework then it is easy with using excel.

So, if you want to read huge number of test data from the excel sheet to feed to the application then we need to know how many rows and columns are there in the excel. If you know exact information about the test data then only we can get the information from the excel in proper way. For this we need to use some third party API which are available. In those one of the famous is Apache POI. In this blog we will use Apache POI to  get the row count and column count from the excel sheet. To interact with the excel, Apache POI given so many classes to interact with the excel and read and write the data. To read/write excel data we will use Java and Apache POI classes.

Below are the classes will use to read/write the data:

  1. FileInputStream – A FileInputStream is an inputstream for reading data from a File.
  2. XSSFWorkbook
  3. XSSFSheet
  4. XSSFRow
  5. XSSFCell





Below are the methods will use to read/write the data which are available in the above classes:

  1. getSheet(“sheetName”) – Get sheet with the given name
  2. getLastCellNum() – Get the index of the last cell contained in the row Plus one as index starts from ZERO
  3. getLastRowNum()

Below is the sample Excel File:

Sample Excel

 

We can download the Apache POI Jar files from this link and can find the screenshots below for the same.

Apache POI Binary Distribution




Apache POI Download Link

 

Points to Remember:

  1. Row index starts from ZERO
  2. Column index starts from ZERO
  3. Extension of excel file is ‘.xlsx’ from Excel 2007

Now we will write a sample stand alone program that will give row count and column count from the excel:

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;

public class ExcelRowAndColumnCount
{
   public static void main(String args[]) throws Exception
   {
       FileInputStream fis = new FileInputStream("/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx");
       XSSFWorkbook workbook = new XSSFWorkbook(fis);
       XSSFSheet sheet = workbook.getSheet("Sheet");
       XSSFRow row = sheet.getRow(0);
       int colNum = row.getLastCellNum();
       System.out.println("Total Number of Columns in the excel is : "+colNum);
       int rowNum = sheet.getLastRowNum()+1;
       System.out.println("Total Number of Rows in the excel is : "+rowNum);
   }
}

In the above program we have hard-coded the row number and sheet name in the program. But for the best practice we will create a util method and will call that method where ever we need. For this we will write 2 classes, one will have the actual logic to get the row count and column count from the excel and another class will call this method.




Below is the excel read/write data util class. In this we will write a method called “getRowCount()” and “getColumnCount()” both will accept single parameter called sheetName from the excel sheet.

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;

public class ExcelApiTest
{
   public FileInputStream fis = null;
   public XSSFWorkbook workbook = null;
   public XSSFSheet sheet = null;
   public XSSFRow row = null;
   public XSSFCell cell = null;
   String xlFilePath;

   public ExcelApiTest(String xlFilePath) throws Exception
   {
       this.xlFilePath = xlFilePath;
       fis = new FileInputStream(xlFilePath);
       workbook = new XSSFWorkbook(fis);
       fis.close();
   }

   public int getRowCount(String sheetName)
   {
       sheet = workbook.getSheet(sheetName);
       int rowCount = sheet.getLastRowNum()+1;
       return rowCount;
   }

   public int getColumnCount(String sheetName)
   {
       sheet = workbook.getSheet(sheetName);
       row = sheet.getRow(0);
       int colCount = row.getLastCellNum();
       return colCount;
   }
}

Below is a sample program which will call the methods from the above program to get the column number and row number from any excel sheet.

public class RowColumnCountFromExcelUsingUtilClass
{
   public static void main(String[] args) throws Exception
   {
       ExcelApiTest eat = new ExcelApiTest("/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx");
       int colCount = eat.getColumnCount("Credentials");
       System.out.println("Total Columns in the Excel : "+colCount);
       int rowCount = eat.getRowCount("Credentials");
       System.out.println("Total Rows in the Excel : "+rowCount);
   }
}

This way we will read or get the row count and column count from the excel sheet.

Please watch youtube video for better and more understanding.



Share this post: on Twitter on Facebook on Google+

Write Data To Excel Using Column Name File Upload using Sikuli in Selenium

Related Posts

WRITE DATA TO EXCEL USING COLUMN NAME

Excel - Java

Write Data To Excel Using Column Name

WRITE DATA TO EXCEL USING COLUMN NUMBER

Excel - Java

Write Data To Excel Using Column Number

READ EXCEL DATA USING COLUMN NAME

Excel - Java

Read Data From Excel Using Column Name

Read Data From Excel Using Column Number

Excel - Java

Read Data From Excel Using Column Number

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2023