Below is the code i used to fetch data,
public String readExcelToFetchCaseIDValue(String fileName, String sheetName) throws IOException {
String caseId = null;
String filePath = FileReaderManager.getInstance().getConfigReader().gettestDataResourcePath() + fileName;
File file = new File(filePath);
// Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
Workbook pegaTestData = null;
// Find the file extension by splitting file name in substring and getting only
// extension name
String fileExtensionName = fileName.substring(fileName.indexOf("."));
// Check condition if the file is xlsx file
if (fileExtensionName.equals(".xlsx")) {
// If it is xlsx file then create object of XSSFWorkbook class
pegaTestData = new XSSFWorkbook(inputStream);
}
// Check condition if the file is xls file
else if (fileExtensionName.equals(".xls")) {
// If it is xls file then create object of HSSFWorkbook class
pegaTestData = new HSSFWorkbook(inputStream);
}
// Read sheet inside the workbook by its name
Sheet pegaDataSheet = pegaTestData.getSheet(sheetName);
// Find number of rows in excel file
int rowCount = pegaDataSheet.getLastRowNum() - pegaDataSheet.getFirstRowNum();
// Create a loop over all the rows of excel file to read it
for (int i = 0; i < rowCount + 1; i++) {
Row row = pegaDataSheet.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
caseId = row.getCell(0).getStringCellValue();
}
}
return caseId;
}