Writing to Excel File using POI API

In the previous post, we have given the overview of Apache POI API like what are these, how to download them etc.In this post, we will learn how to use POI API to write the data into Excel Files.

We will be using (.xlsx) format, for which we require implementation of XSSF API. Let’s understand the procedure step by step.

Step#1 It is assumed that you have downloaded required jars and uploaded to your class ( as shown in previous post). As we know, an excel workbook contains the components like workbook, sheet, rows and cells. In Selenium also , we need to do the same with the help of coding. First we will create the objects like  XSSFWorkbook, XSSFSheet.XSSFRow,XSSFCell etc.

Step#2 First , we will create one Excel file instance and a sheet under this named as ‘Data’.

Step#3 We need some data which will be written into the excel sheet. We have created some dummy data with the help of  2 dimensional array of String type. This data , we will write into the Excel sheet. (You can have data according to your requirement).

Step#4 We will count the no. of rows and columns need to be filled up with the data. For that, we will count the length of array.

Step#5 Next, we need to create the row and column to set the value into it. For that , we will use the methods mentioned below.


Step#6 We will use FileOutputStream method to flush the data to the Excel file and File path will be provided in the arguments. We use the write method to write the data into Excel file mentioned in FileOutputStream.

After that close the file output steam.

Full Code to write the data into Excel Files:


 Excel writing poi









Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Introduction to Apache POI API
Reading from Excel using POI API
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

3 Responses

  1. Suresh says:

    very nice and thanks for your support.

  2. Daniel says:

    Very useful. How can we store a data(multiple) fetched from a website in an Excel? All I can do is write only one data in the excel, I am doing something wrong in the loop part.

    • Can you elaborate more what exactly you are trying to achieve? You should validate data first , If you have all required data, split it at desired delimiter and see the count that needs to be written in the excel. If it is more than one, you have a loop till that count.

Leave a Reply

Your email address will not be published. Required fields are marked *