How to get row count,column count in Excel useful for selenium
In this post I am going to explain how can we get row count,column count in excel file with the help of Java using Apache POI jar libraries.
Download Apache POI jar libraries in following link and configure to your project.
Download Apache POI jar
Sample code:
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
public class Xls_Reader
{
public String path;
public FileInputStream fis = null;
public FileOutputStream fileOut =null;
private XSSFWorkbook workbook = null;
private XSSFSheet sheet = null;
private XSSFRow row =null;
private XSSFCell cell = null;
public static String sActionKeyword=null;
//Constructor for path configuration
public Xls_Reader(String path)
{
this.path=path;
try
{
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
fis.close();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// returns number of rows in a sheet
public int getRowCount(String sheetName)
{
int index = workbook.getSheetIndex(sheetName);
if(index==-1)
return 0;
else
{
sheet = workbook.getSheetAt(index);
int number=sheet.getLastRowNum()+1;
return number;
}
}
// returns number of columns in a sheet
public int getColumnCount(String sheetName)
{
// check if sheet exists
if(!isSheetExist(sheetName))
return -1;
sheet = workbook.getSheet(sheetName);
row = sheet.getRow(0);
if(row==null)
return -1;
return row.getLastCellNum();
}
// find whether sheets exists
public boolean isSheetExist(String sheetName)
{
int index = workbook.getSheetIndex(sheetName);
if(index==-1){
index=workbook.getSheetIndex(sheetName.toUpperCase());
if(index==-1)
return false;
else
return true;
}
else
return true;
}
//usage
public static void main(String[] args){
Xls_Reader xls = new Xls_Reader("C:/Users/Sudharsan/Desktop/TestData.xlsx");
System.out.println(xls.isSheetExist("Login"));
System.out.println(xls.getRowCount("Login"));
System.out.println(xls.getColumnCount("Login"));
}
}
In this post I am going to explain how can we get row count,column count in excel file with the help of Java using Apache POI jar libraries.
Download Apache POI jar libraries in following link and configure to your project.
Download Apache POI jar
Sample code:
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
public class Xls_Reader
{
public String path;
public FileInputStream fis = null;
public FileOutputStream fileOut =null;
private XSSFWorkbook workbook = null;
private XSSFSheet sheet = null;
private XSSFRow row =null;
private XSSFCell cell = null;
public static String sActionKeyword=null;
//Constructor for path configuration
public Xls_Reader(String path)
{
this.path=path;
try
{
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
fis.close();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// returns number of rows in a sheet
public int getRowCount(String sheetName)
{
int index = workbook.getSheetIndex(sheetName);
if(index==-1)
return 0;
else
{
sheet = workbook.getSheetAt(index);
int number=sheet.getLastRowNum()+1;
return number;
}
}
// returns number of columns in a sheet
public int getColumnCount(String sheetName)
{
// check if sheet exists
if(!isSheetExist(sheetName))
return -1;
sheet = workbook.getSheet(sheetName);
row = sheet.getRow(0);
if(row==null)
return -1;
return row.getLastCellNum();
}
// find whether sheets exists
public boolean isSheetExist(String sheetName)
{
int index = workbook.getSheetIndex(sheetName);
if(index==-1){
index=workbook.getSheetIndex(sheetName.toUpperCase());
if(index==-1)
return false;
else
return true;
}
else
return true;
}
//usage
public static void main(String[] args){
Xls_Reader xls = new Xls_Reader("C:/Users/Sudharsan/Desktop/TestData.xlsx");
System.out.println(xls.isSheetExist("Login"));
System.out.println(xls.getRowCount("Login"));
System.out.println(xls.getColumnCount("Login"));
}
}
Hi Jagan,
ReplyDeleteYour methods for excel looks good. If you have shown excel sheet it will be much helpful to see the blogs you posted.
Regards,
Vishwaa.
Hi,
ReplyDeleteYou have written very nice article. I acquired good knowledge about Selenium. I will follow up your blog for future post.
Regards,
best Selenium online training in Hyderabad, India
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
ReplyDeleteselenium training in bangalore|
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThose guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
ReplyDelete"Selenium Training in Marathahalli"
hi, your post is good but is there any way to find out the number of filled rows.
ReplyDeleteHow to get the total number of rows for the 5th column in excel using java?
ReplyDelete