Tuesday, 19 January 2016

How to read Excel file using java for Selenium

To read an excel file using java we need to download 4 jar files which will help to read excel files. So whenever you are starting to read the file below is the list of jar files to be used:

1. dom4j<latest ver>.jar
2. poi<latest ver>.jar
3. poi ooxml <latest ver>.jar
4. poi ooxml shcemas <latest ver>.jar

As soon as you configure these jar files in your build path, below code can be used which can extract data from excel files:

        FileInputStream ExcelFile = new FileInputStream("<File Name>.xlsx");
        XSSFWorkbook ExcelWBook = new XSSFWorkbook(ExcelFile);
  XSSFSheet ExcelWSheet = ExcelWBook.getSheet(SheetName);
      XSSFCell Cell = ExcelWSheet.getRow(rownum).getCell(column);
        String Celldata = Cell.getStringCellValue();

XSSF is used for .xlsx files and HSSF is used for .xls files.