Dataprovider:- Getting data from excel sheet using Selenium Webdriver
Class Name:- DataInputProvider.java
public class
DataInputProvider {
public static ArrayList<ArrayList<String>> getSheet(String dataSheetName, String SheetName) throws IOException{
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
try {
FileInputStream fis = new FileInputStream(new File(System.getProperty("user.dir")+"\\data\\"+dataSheetName+".xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheet(SheetName);
// get the number of rows
int rowCount = sheet.getLastRowNum();
// get the number of columns
int columnCount = sheet.getRow(0).getLastCellNum();
// loop through the rows
for(int i=1; i <rowCount+1; i++){
try {
XSSFRow row = sheet.getRow(i);
ArrayList<String> record = new ArrayList<String>();
for(int j=0; j <columnCount; j++){ // loop through the columns
try {
record.add(row.getCell(j).getStringCellValue()); // add to the record
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
data.add(record);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// workbook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}
Comments
Post a Comment