Write to Excel File in Java Using Apache POI

In this article we’ll see how to write to Excel file in Java using Apache POI library. If you have to read an Excel file in Java you can check this post- Read Excel File in Java Using Apache POI

Apache POI

Apache POI is a Java API for Microsoft documents. Using Apache POI you can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java.

Apache POI provides support for reading both OLE2 files and Office Open XML standards (OOXML) files.

  • OLE2 files include most Microsoft Office files such as XLS, DOC, and PPT as well as MFC serialization API based file formats.
  • Office OpenXML Format is the new standards based XML file format found in Microsoft Office 2007 and 2008. This includes XLSX, DOCX and PPTX.

Which means Apache POI supports writing excel files having .xls format as well as excel files with .xlsx format.

Maven dependencies for Apache POI

To write to excel file using Apache POI you need to add the following dependency.

This dependency adds the following jars-

Here note that poi-4.0.1.jar has classes for working with old excel format (.xls)
and poi-ooxml-4.0.1.jar has classes for working with newer excel format (.xlsx). Apache POI 4.0.1 requires Java 8 or newer.

Apache POI classes for writing excel files

Following section gives an overview of classes that are used for writing to excel file in Java using Apache POI.
Apache POI library provides two implementations for two excel spread sheet formats-

  • HSSF– It is the pure Java implementation for earlier excel format (.xls). Classes in this implementation will usually have HSSF prefix like HSSFWorkBook, HSSFSheet.
  • XSSF– It is the pure Java implementation for xslx file format (OOXML). Classes in this implementation will usually have XSSF prefix like XSSFWorkBook, XSSFSheet.

SS– It is a package built on top of HSSF and XSSF that provides common support for both formats with a common API. You should try to use classes from this package for better compatibility.

While working with excel file the usual progression is-

write to excel file in Java
There are interfaces corresponding to this progression-

  • org.apache.poi.ss.usermodel.Workbook– High level representation of a Excel workbook. This is the first object most users will construct whether they are reading or writing a workbook. It is also the top level object for creating new sheets.
  • org.apache.poi.ss.usermodel.Sheet– High level representation of a Excel worksheet. Sheets are the central structures within a workbook.
  • org.apache.poi.ss.usermodel.Row– High level representation of a row of a spreadsheet.
  • org.apache.poi.ss.usermodel.Cell– High level representation of a cell in a row of a spreadsheet. Cells can be numeric, formula-based or string-based (text).

For creating a Workbook, WorkbookFactory class is used.

  • org.apache.poi.ss.usermodel.WorkbookFactory– Factory for creating the appropriate kind of Workbook (be it HSSFWorkbook or XSSFWorkbook), by auto-detecting from the supplied input.

Write excel file in Java using Apache POI example

For the example there is a class Employee with fields firstName, lastName, department and DOB. Using these fields data is written to excel file in 4 columns per row.
Employee class

Following Java class retrieve data from fields of Employee objects and write to excel sheet.

Running this program creates excel sheet as shown below.

excel file in Java

That’s all for the topic Write to Excel File in Java Using Apache POI. If something is missing or you have something to share about the topic please write a comment.


You may also like

  • How to List All The Files in a Directory in Java
  • Java Program to Convert Numbers to Words
  • Static Keyword in Java
  • Difference Between Interface And Abstract Class in Java
  • Different Ways to Iterate a HashMap in Java
  • HDFS Replica Placement Policy
  • Leave a Reply

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

    This site uses Akismet to reduce spam. Learn how your comment data is processed.