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:
- FileInputStream – A FileInputStream is an inputstream for reading data from a File.
- XSSFWorkbook
- XSSFSheet
- XSSFRow
- XSSFCell
Below are the methods will use to read/write the data which are available in the above classes:
- getSheet(“sheetName”) – Get sheet with the given name
- getLastCellNum() – Get the index of the last cell contained in the row Plus one as index starts from ZERO
- getLastRowNum()
Below is the sample Excel File:
We can download the Apache POI Jar files from this link and can find the screenshots below for the same.
Points to Remember:
- Row index starts from ZERO
- Column index starts from ZERO
- 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.