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.

Syntax:

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:

Output:

 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. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

1 Response

  1. Suresh says:

    very nice and thanks for your support.

Leave a Reply

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