Friday, March 4, 2011

Data driven test using multiple excel sheets

use jxl.jar

public String[] getExcelDataByRow(String sheetName, int rowNum){
       //Create a dynamic array
       String[] retVal=null;
       try{
           Workbook w=Workbook.getWorkbook(new File("c:/data.xls));
           Sheet s=w.getSheet(sheetName);
           //get the number of columns in the excel sheet
           int colCount=s.getColumns();
           //fix the size of the array with the col count
           retVal=new String[colCount];
           //loop through all the columns and store the value into
the array
           for(int i=0;i<colCount;i++){
               retVal[i]=s.getCell(i, rowNum-1).getContents();
           }
       }
       catch(Exception e){
           e.printStackTrace();
       }
       return retVal;
   }

--------------------------------------------
call the above method
String[] dataArr=dl.getExcelDataByRow("Login", 2);

Login is sheet name

No comments:

Post a Comment