How To Read an Excel File Using Apache POI In Selenium WebDriver

Apache provides a library POI using which read and write of an excel can be performed in Selenium WebDriver. Using it the read and write operation of both xls and xlsx file formats can be done. So first you need to download the Apache POI and configure it (jar files) in your eclipse. Implement the following steps to read an excel file:

1) Launch eclipse, create a project, a package, and a class inside this package.

2) Add all the jar files of Apache POI to your project through java build path.

3) Add all the jar files inside Apache poi folder.

4) Then go to lib folder under the Apache poi folder and all the jar files present inside it.

5) Then go to ooxml-lib folder and all the jar files present inside it.

6) Now you can proceed with your script. Go to the created class and add the below given lines of code. But before this you need to create an excel sheet in any of your local drive folder and insert some data inside it. Name the sheet as Data and rename the sheet inside this file as “Input”. Save this excel sheet in .xlsx format.

Here the first line takes the path where you have created your excel sheet and then pass it in the workbook object. It will create a pipeline between the sheet and the eclipse. Then we can use the getSheet method which is present inside the Workbook class where we have to pass the sheet name. Here the sheet name is “Input”.

8) Now you can just give the row number and the cell number in the methods of the respective cell which value you want to display.