How to read the data from the excel file? And what are the jar files which need to be imported for reading the excel file ?

Default Asked on September 9, 2015 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)

    Hi asim …. Download the Poi API and import all the jars available under the package.

    and for reading the data from excel you can use this code

    public class random {

    // XSSF for .xlsx and HSSF for .xls format

    public static XSSFSheet ExcelWsheet;
    public static XSSFWorkbook ExcelWBook;
    private static XSSFCell cell;
    private static XSSFRow Row;

    public static void setExcelFile() throws IOException
    {
    try {
    FileInputStream ExcelFile =new FileInputStream(Write Excel sheet path);
    ExcelWBook =new XSSFWorkbook(ExcelFile);
    ExcelWsheet = ExcelWBook.getSheetAt(0); // sheet number
    }
    catch(Exception e)
    {
    throw(e);
    }
    }

    //This method is to read the test data from the Excel cell, in this we are passing parameters as Row num and Col num

    public static void getCellData()
    {
    try
    {
    int lastRow =ExcelWsheet.getLastRowNum()+1;
    for(int i=0;i<lastRow;i++)
    {
    cell =ExcelWsheet.getRow(i).getCell(0);
    String CellData=cell.getStringCellValue();
    System.out.println(CellData);
    }
    }
    catch (Exception e)
    {
    }
    }
    }

    Default Answered on September 10, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.