I am new to Java programming and I am writing a method to retrieve all the rows from excel worksheet using apache POI. I have created a separate class called ' ExcelDataProvider' where I am calling the method to pull values from the worksheet. The method that I wrote is working fine for the first row but it's not processing all the rows. Here is the code that I wrote...
public static String getCellDataNew(String sheetName, String colName, int rowNum) {
try {
int col_Num = 0;
ExcelWSheet = ExcelWBook.getSheet(sheetName);
Row = ExcelWSheet.getRow(0);
int rowcount = ExcelWSheet.getLastRowNum();
for (int i = 0; i < Row.getLastCellNum(); i++) {
if (Row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
col_Num = i;
}
for (rowNum = 2; rowNum <= rowcount; rowNum++) {
Row = ExcelWSheet.getRow(rowNum - 1);
Cell = Row.getCell(col_Num);
}
if (Cell.getCellType() == CellType.STRING)
return Cell.getStringCellValue();
else if (Cell.getCellType() == CellType.NUMERIC || Cell.getCellType() == CellType.FORMULA) {
String cellValue = String.valueOf(Cell.getNumericCellValue());
return cellValue;
} else if (Cell.getCellType() == CellType.BLANK)
return "";
else
return String.valueOf(Cell.getBooleanCellValue());
} catch (Exception e) {
e.printStackTrace();
return "row " + rowNum + " or column does not exist in Excel";
}
}
.....................................................
Here is the code from dataprovider class
package utility;
public class ExcelDataProvider {
private String sDealer;
private String sBranchCode;
private String sBranchName;
private String sAccountType;
private String sProductSelection;
private String sTaxResidence;
private String sJointOwnershipOption;
private String sJointOwnershipType;
private String sJointSignatureOption;
private String sJOSelectionLastName;
private String sJOSelectionFirstName;
public ExcelDataProvider() throws Exception {
this.setsDealer(Excelutil.getCellDataNew("OpenAccount", "Dealer", 2));
this.setsBranchCode(Excelutil.getCellDataNew("OpenAccount", "BranchCode", 2));
this.setsBranchName(Excelutil.getCellDataNew("OpenAccount", "BranchName", 2));
this.setsAccountType(Excelutil.getCellDataNew("OpenAccount", "AccountType", 2));
this.setsProductSelection(Excelutil.getCellDataNew("OpenAccount", "ProductSelection", 2));
this.setsTaxResidence(Excelutil.getCellDataNew("OpenAccount", "TaxResidence", 2));
this.setsJointOwnershipOption(Excelutil.getCellDataNew("OpenAccount", "JointOwnership", 2));
this.setsJointOwnershipType(Excelutil.getCellDataNew("OpenAccount", "JointOwnershipType", 2));
this.setsJointSignatureOption(Excelutil.getCellDataNew("OpenAccount", "JointSignature", 2));
this.setsJOSelectionLastName(Excelutil.getCellDataNew("OpenAccount", "JointOwnerSelectionLastName", 2));
this.setsJOSelectionFirstName(Excelutil.getCellDataNew("OpenAccount", "JointOwnerSelectionFirstName", 2));
}
public String getsDealer() {
return this.sDealer;
}
public String getsBranchCode() {
return this.sBranchCode;
}
public String getsBranchName() {
return this.sBranchName;
}
public String getsAccountType() {
return this.sAccountType;
}
public String getsProductSelection() {
return this.sProductSelection;
}
public String getsTaxResidence() {
return sTaxResidence;
}
public String getsJointOwnershipOption() {
return sJointOwnershipOption;
}
public String getsJointOwnershipType() {
return sJointOwnershipType;
}
public String getsJointSignatureOption() {
return sJointSignatureOption;
}
public String getsJOSelectionLastName() {
return sJOSelectionLastName;
}
public String getsJOSelectionFirstName() {
return sJOSelectionFirstName;
}
public void setsDealer(String sDealer) {
this.sDealer = sDealer;
}
public void setsBranchCode(String sBranchCode) {
this.sBranchCode = sBranchCode;
}
public void setsBranchName(String sBranchName) {
this.sBranchName = sBranchName;
}
public void setsAccountType(String sAccountType) {
this.sAccountType = sAccountType;
}
public void setsProductSelection(String sProductSelection) {
this.sProductSelection = sProductSelection;
}
public void setsTaxResidence(String sTaxResidence) {
this.sTaxResidence = sTaxResidence;
}
public void setsJointOwnershipOption(String sJointOwnershipOption) {
this.sJointOwnershipOption = sJointOwnershipOption;
}
public void setsJointOwnershipType(String sJointOwnershipType) {
this.sJointOwnershipType = sJointOwnershipType;
}
public void setsJointSignatureOption(String sJointSignatureOption) {
this.sJointSignatureOption = sJointSignatureOption;
}
public void setsJOSelectionLastName(String sJOSelectionLastName) {
this.sJOSelectionLastName = sJOSelectionLastName;
}
public void setsJOSelectionFirstName(String sJOSelectionFirstName) {
this.sJOSelectionFirstName = sJOSelectionFirstName;
}
}
This is how my spreadsheet looks like..
Testcase| ClientName| Dealer| BranchCode| BranchName| AccountType|ProductSelection| TaxResidence| JointOwnership | JointOwnershipType | JointSignature| JointOwnerSelectionLastName| JointOwnerSelectionFirstName
1 |Test, GR| NBIN Introducing| HOUS| NBIN Intro| Non Registered| Cash | Ontario| Yes| Joint WROS| Yes| Test| ABC
2 |Test, GR| NBIN Introducing| HOUS| NBIN Intro| Non Registered| Cash | Ontario| Yes| Joint WROS| Yes| Test| ABC