Friday, 23 August 2013

Selenium : Read Excel file using JExcel API

Code to read  value from Excel file using selenium WebDriver & Java is as follows -

inputWorkbook = new File("C:\\Controller.xls"); //specify excel file path

    Workbook w;
               
    try {
               
            try {

                        w = Workbook.getWorkbook(inputWorkbook);

                        sheet = w.getSheet("Sheet1"); //Worksheet name

                        } catch (IOException e) {
                                                                       
                                    error("Reading Master Sheet :-" +e);
                        }
                 
                        suite = new String[sheet.getRows()][sheet.getColumns()];
              
                        for (int j = 0; j < sheet.getColumns(); j++){
                                   
                                    for (int i = 0; i < sheet.getRows(); i++){
                     
                                                Cell cell = sheet.getCell(j, i);
                     
                                                if (cell.getContents()!= null){
                         
                                                            suite[i][j]=cell.getContents();
                                                }
                   
                                    }
                 
                        }
              
Note – Suite[][] is two dimensional array which stores value read from Excel Worksheet.

No comments:

Post a Comment