Category:
Selenium WebDriver
//Create an object of File class to open xlsx file
File file = new File(filePath+""\\""+fileName);
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
Workbook workBook = null;
//Find the file extension by splitting file name
String fileExtensionName = fileName.substring(fileName.indexOf("".""));
//Check condition if the file is xlsx file
if(fileExtensionName.equals("".xlsx"")){
workBook = new XSSFWorkbook(inputStream);
} else if(fileExtensionName.equals("".xls"")){
workBook = new HSSFWorkbook(inputStream);
}
//Read sheet inside the workbook by its name
Sheet sheet = workBook.getSheet(sheetName);
//Find number of rows in excel file
int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
//Create a loop over all the rows of excel file to read it
for (int i = 0; i < rowCount+1; i++) {
Row row = sheet.getRow(i);
//Create a loop to print cell values in a row
for (int j = 0; j < row.getLastCellNum(); j++) {
//Print Excel data in console
System.out.print(row.getCell(j).getStringCellValue()+""|| "");