ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to read Excel file using Selenium WebDriver

Below is a basic code on how to read excel file. As per your requirement you can read the data for excel and play around it.

  1. import java.io.File;
  2.  
  3. import java.io.FileInputStream;
  4.  
  5. import java.io.IOException;
  6.  
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8.  
  9. import org.apache.poi.ss.usermodel.Row;
  10.  
  11. import org.apache.poi.ss.usermodel.Sheet;
  12.  
  13. import org.apache.poi.ss.usermodel.Workbook;
  14.  
  15. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16.  
  17. public class ReadExcelFile{
  18.  
  19. public void readExcel(String filePath,String fileName,String sheetName) throws IOException{
  20.  
  21. //Create a object of File class to open xlsx file
  22.  
  23. File file =    new File(filePath+"\\"+fileName);
  24.  
  25. //Create an object of FileInputStream class to read excel file. It accept File object as argument
  26.  
  27. FileInputStream inputStream = new FileInputStream(file);
  28.  
  29. Workbook workbook= null;
  30.  
  31. //Find the file extension by spliting file name in substring and getting only extension name
  32.  
  33. // Excel comes in two different extension. Both kind of extension type need to be handled in a different manner
  34.  
  35. String fileExtensionName = fileName.substring(fileName.indexOf("."));
  36.  
  37. //Check condition if the file is xlsx file
  38.  
  39. if(fileExtensionName.equals(".xlsx")){
  40.  
  41. //If it is xlsx file then create object of XSSFWorkbook class
  42.  
  43. workbook= new XSSFWorkbook(inputStream);
  44.  
  45. }
  46.  
  47. //Check condition if the file is xls file
  48.  
  49. else if(fileExtensionName.equals(".xls")){
  50.  
  51. //If it is xls file then create object of XSSFWorkbook class
  52.  
  53. workbook= new HSSFWorkbook(inputStream);
  54.  
  55. }
  56.  
  57. //Read sheet inside the workbook by its name
  58.  
  59. Sheet sheet = Workbook.getSheet(sheetName);
  60.  
  61. //Find number of rows in excel file
  62.  
  63. int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
  64.  
  65. //Create a loop over all the rows of excel file to read it
  66.  
  67. for (int i = 0; i < rowCount+1; i++) {
  68.  
  69. Row row = sheet.getRow(i);
  70.  
  71. //Create a loop to print cell values in a row
  72.  
  73. for (int j = 0; j < row.getLastCellNum(); j++) {
  74.  
  75. //Print excel data in console
  76.  
  77. System.out.print(row.getCell(j).getStringCellValue()+"|| ");
  78.  
  79. }
  80.  
  81. System.out.println("Success");
  82.  
  83. }
  84.  
  85. }
  86.  
  87. //Main function is calling readExcel function to read data from excel file
  88.  
  89. public static void main(String...strings) throws IOException{
  90.  
  91. //Create a object of ReadExcelFile class
  92.  
  93. ReadExcelFile objExcelFile = new ReadExcelFile();
  94.  
  95. //Prepare the path of excel file
  96.  
  97. String filePath = System.getProperty("user.dir")+"\\filepath";
  98.  
  99. //Call read file method of the class to read data
  100.  
  101. objExcelFile.readExcel(filePath,"ExportExcel.xlsx","ExcelDemo");
  102.  
  103. }
  104.  
  105. }
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!