Read & Write Data from Excel File using Java with Apache POI for Selenium Data Driven Framework

=====================================================================

  1. Download and Setup Apache POI
  2. Read Excel file using Apache POI
  3. Write Excel file using Apache POI
  4. Download input.xlsx file – input.xlsx
  5. Download Full Data Driven Frmework project – Excel_Read_Write

=====================================================================

1. Download and Setup Apache POI

=====================================================================

Apache POI:

Apache POI is an API, It is freeware & written in Java, using apache POI able to read/write Excel files because of so many predefined methods, classes, and interfaces.

——————————————————————————–

Two ways to Read and Write excel file in Selenium project using Java:

Read and Write Excel file Using two ways using Jar File or using maven dependency:

  1. Download apache poi jar file add into project libraries.

                                                 OR

  1. Using Maven Add poi-ooxml dependency into Maven pom.xml file.

——————————————————————————–

Method 1: Download Apache POI to Read and Write in Excel file:

  1. Go to Apache POI download page to download the Apache POI jar file.

URLhttp://poi.apache.org/download.html

  1. Under Available Downloads Click on the latest stable release is Apache POI — link.

URLhttps://poi.apache.org/download.html#POI-4.1.0

  1. Under Binary Distribution click on Zip file to Download Apache POI jar file.
  2. Then Click on the first highlighted link at the top of the page.
  3. After file downloading Right click on the Zip file and select “Extract All“.

——————————————————————————–

Add Apache POI Jars into Project Library:

  1. Right, click on the project.
  2. Click on Properties.
  3. Click on Java Build Path.
  4. Select the Libraries Tab.
  5. Click on Add External Jars.
  6. Select Apache POI Jar files.
  7. Click on Apply and Close button.

——————————————————————————–

Method 2: Using poi-ooxml Maven Add poi-ooxml dependency into Maven pom.xml file.

=====================================================================

2. Read Excel file using Apache POI

=====================================================================

  • To Read data from excel we will use Java and Apache POI classes.
  • Below are the classes we will use to read the data from the Excel file.

——————————————————————————–

  • FileInputStream – A FileInputStream is Use to read data from a file in the form of a sequence of bytes. XSSFWorkbook – HSSF and XSSF provide ways to read spreadsheets create, modify, read and write XLS spreadsheets.
  • XSSFSheet – Use to create a new sheet.
  • XSSFRow – Read / Write Row.
  • XSSFCell – Read / Write Cell using Row and Column Number.

——————————————————————————–

  • Row index starts from ZERO (0).
  • Column index starts from ZERO (0).
  • ‘.xlsx’ Extension of excel file from Excel 2007 to till now. 

——————————————————————————–

  • Test data contains numeric, string Data and sometimes combination as well which is totally dynamic.
  • So we create such library which able to handle dynamic data from excel file.
  • If we want to read String value you can call getStringCellValue() method
  • If we want to read Numeric value you can call getNumericCellValue() sometimes which will return a double value which we can typecast into int and then we can use in our test data.

 

——————————————————————————–

Output:-

String Fetch from Excel:- Username

Number Fetch from Excel:- 123456789

——————————————————————————–

Read Cell Data from Excel:-

 

=====================================================================

3. Before Write Excel file using Apache POI

=====================================================================

  • Write data into excel file we will use Java and Apache POI classes.
  • Below are the classes we will use to Write the data In Excel file Cell.

——————————————————————————–

  • FileOutputStream – To write the data to file, you have to convert the data into bytes and save it to file.
  • FileInputStream – A FileInputStream is Use to read data from a file in the form of a sequence of bytes. XSSFWorkbook – HSSF and XSSF provide ways to read spreadsheets create, modify, read and write XLS spreadsheets.
  • XSSFSheet – Use to create a new sheet.
  • XSSFRow – Read / Write Row.
  • XSSFCell – Read / Write Cell using Row and Column Number.

——————————————————————————–

  • Row index starts from ZERO (0).
  • Column index starts from ZERO (0).
  • ‘.xlsx’ Extension of excel file from Excel 2007 to Till now.

——————————————————————————–

——————————————————————————–

After Write Data in Excel File

Package / Project Structure –