Friday 8 May 2015

Selenium:How to read data from excel sheet in selenium webdriver

package exceltest;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Dara {

    /*
     * public static void fileReading(String filepath) throws
     * FileNotFoundException{
     *
     * FileInputStream fis =new FileInputStream(filepath);
     *
     *
     * }
     */
    static Cell c;
    static Row r;

    public static void main(String[] args) throws InvalidFormatException,
            IOException {
        FileInputStream fis = new FileInputStream(
                "C:\\Users\\RAMESH\\workspace\\ramesh_projects\\Excel\\src\\testdata\\abcd.xls");

        // fileReading("C:\\Users\\IN00914\\workspace\\ramesh_projects\\Excel\\src\\testdata\\abcd.xls");
        // fileReading();

        // FileInputStream fis1 =new FileInputStream("");
        Workbook w = WorkbookFactory.create(fis);
        Sheet s = w.getSheet("Sheet1");
        /*
         * r=s.getRow(0); c=r.getCell(0); String
         * celldata1=c.getStringCellValue(); System.out.println(celldata1);
         * c=r.getCell(1); System.out.println(c.getStringCellValue());
         */

        for (int i = 0; i < s.getLastRowNum(); i++) {

            r = s.getRow(i);
            for (int j = 0; j < r.getLastCellNum(); j++) {

                r.getCell(j).getStringCellValue();
                System.out.println(r.getCell(j).getStringCellValue());
            }

        }

        /*
         * r=s.getRow(1); c=r.getCell(0); String
         * celldata=c.getStringCellValue(); System.out.println(celldata);
         * c=r.getCell(1); System.out.println(c.getStringCellValue());
         */
    }

}

No comments:

Post a Comment