How to Write XLS and XLSX File in Java – Apach POI Example

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

In this tutorial, You will see How can you write the XLS or XLSX files in Java using Apache POI. Java doesn’t provide direct API to read and write Excel document, you have to rely on third party libraries. In the last article, We have seen How to read .xls or .xlsx file in java. To read and write XLS or XLSX file you can use JXL open source library or Apache POI library.

 

I will suggest to Apache POI instead of JXL because JXL supports only .XLS format(produced by Microsoft Officer version prior to 2007 e.g. MS Office 2000 and 2003) on the other hand Apache POI supports both formats .XLS (produced by Microsoft Officer version prior to 2007 e.g. MS Office 2000 and 2003) and .XLSX (created by Microsoft Office 2007 onwards e.g. MS Office 2010 and 2013).

With Apache POI, you can easily create, read, write and update Excel files using this library. It uses terms like workbook, worksheet, cell, row to keep itself aligned with Microsoft Excel and that’s why it is very easy to use.

 

HOW TO CONFIGURE APACHE POI IN JAVA PROJECT

Before starting with Apache POI. First, you need to configure the POI libraries with your project. To configure the Apache POI, follow the below-mentioned steps –

  1. Download Apache POI from apache’s Offical website.
  2. You will get the .zip file. Unzip the file.
  3. Keep it at some secure place.
  4. Add all libraries (.jar files) inside your project as External jar files.

Done, You have configured it.

 

Write .XLSX FILE USING APACHE POI

Let’s see how can you write the .xlsx file using Apache POI.

Writing a file using POI is very simple and involve following steps:

  1. Create a workbook
  2. Create a sheet in workbook
  3. Create a row in sheet
  4. Add cells in sheet
  5. Repeat step 3 and 4 to write more data

It seems very simple, right? Let’s have a look at the code doing these steps:

Above code will write SampleExcel.xlsx file inside my D Drive and create the sheet named Employee Data inside the .xlsx file and write the above given data inside the Employee Data Sheet.

 

Write .XLS FILE USING APACHE POI

Let’s see how can you write the .xls file using Apache POI.

To write .xls use HSSF instead of XSSF.

Writing a file using POI is very simple and involve following steps:

  1. Create a workbook
  2. Create a sheet in workbook
  3. Create a row in sheet
  4. Add cells in sheet
  5. Repeat step 3 and 4 to write more data

It seems very simple, right? Let’s have a look at the code doing these steps:

This will write the SampleExce.xls file inside your D Drive and save it.

Have you noticed the difference in writing the .xlsx and .xls file. The only difference was in WorkBook and WorkSheet object class.

While writing the .xlsx file we were using XSSF (XML SpreadSheet Format) to writing XML (XLSX) format files and while writing the .xls version we were using HSSF (Horrible SpreadSheet Format)to read  Microsoft Excel (XLS) format files.

That’s all, How can we write the .xls and .xlsx file using Apache POI. In next tutorial, You will see, How to read XLS and XLSX files in Java using Apache POI.

0 Comment

Leave a Reply