Java provides us different classes for File Manipulation with Selenium. In this tutorial, we are going to learn how can we read and write on Excel file with the help of Java IO package and Apache POI library.

How to handle excel file using POI (Maven POM Dependency) Classes and Interfaces in POI Read/Write operation Read data from Excel file Write data on Excel file Excel Manipulation using JXL API

Exporting Excel

How to handle excel file using POI (Maven POM Dependency)

	To Read and Write Excel file in Java, Apache provides a very famous library POI. This library is capable enough to read and write both XLS and XLSX file format of Excel.

To read XLS files, an HSSF implementation is provided by POI library. To read XLSX, XSSF implementation of POI library will be the choice. Let’s study these implementations in detail. If you are using Maven in your project, the Maven dependency will be

Or you can simply download the latest version POI jars from http://poi.apache.org/download.html & download the latest zip file

When you download the zip file for this jar, you need to unzip it and add these all jars to the class path of your project.

Classes and Interfaces in POI:

Following is a list of different Java Interfaces and classes in POI for reading XLS and XLSX file-

Workbook: XSSFWorkbook and HSSFWorkbook classes implement this interface.

XSSFWorkbook: Is a class representation of XLSX file.

HSSFWorkbook: Is a class representation of XLS file.

Sheet: XSSFSheet and HSSFSheet classes implement this interface.

XSSFSheet: Is a class representing a sheet in an XLSX file.

HSSFSheet: Is a class representing a sheet in an XLS file.

Row: XSSFRow and HSSFRow classes implement this interface.

XSSFRow: Is a class representing a row in the sheet of XLSX file.

HSSFRow: Is a class representing a row in the sheet of XLS file.

Cell: XSSFCell and HSSFCell classes implement this interface.

XSSFCell: Is a class representing a cell in a row of XLSX file.

HSSFCell: Is a class representing a cell in a row of XLS file.

Read/Write operation-

For our example, we will consider below given Excel file format

Read data from Excel file

Complete Example: Here we are trying to read data from Excel in Selenium: Note: We are not using the Testng framework here. Run the class as Java Application using function read excel in Selenium as shown in above example.

Write data on Excel file

Complete Example: Here we are trying to write data from Excel file by adding new row in Excel file

Excel Manipulation using JXL API

JXL is also another famous jar to read Excel file in Java and writing files. Nowadays, POI is used in most of the projects, but before POI, JXL was only Java API for Excel manipulation. It is a very small and simple API for excel reading in Selenium.

TIPS: My suggestion is not to use JXL in any new project because the library is not in active development from 2010 and lack of the feature in compare to POI API. Download JXL: If you want to work with JXL, you can download it from this link https://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/

You can also get demo example inside this zipped file for JXL.

Some of the features:

	JXL is able to read Excel file in Selenium for 95, 97, 2000, XP, 2003 workbook.

	We can work with English, French, Spanish, German.

	Copying a Chart and image insertion in Excel is possible

Drawback:

	We can write Excel 97 and later only (writing in Excel 95 is not supported).

	JXL does not support XLSX format of excel file.

	It Generates spreadsheet in Excel 2000 format.

Summary:

	Excel file can be read by Java IO operation. For that, we need to use Apache POI Jar.

	There are two kinds of a workbook in Excel file, XLSX and XLS files.

	POI has different Interfaces Workbook, Sheet, Row, Cell.

	These interfaces are implemented by corresponding XLS (HSSFWorkbook, HSSFSheet, HSSFRow, HSSFCell) and XLSX (XSSFWorkbook, XSSFSheet, XSSFRow, XSSFCell) file manipulation classes.

	JXL is another API for Excel handling in Selenium.

	JXL cannot work with XLSX format of excel.