How do I write to an Excel file using POI?
Author: Deron Eriksson
Description: This Java tutorial describes how to write to an Excel file using Apache POI.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1

Page: < 1 2

(Continued from page 1)

Cells can contain several different types of data, and they can also be formatted in a variety of ways. For more information, I highly recommend the HSSF Quick Guide mentioned above. In we run the PoiWriteExcelFile class, the poi-test.xls file gets written to the root level of our project. If we open this file with Excel, we see the following result.

poi-test.xls in Excel

One thing I find very useful is to create some helper methods to work with the POI-HSSF classes. For example, I find it rather annoying to have to cast cell rows and columns to 'shorts', and also I find it confusing to index rows and columns from 0, when columns on the Excel worksheet index alphabetically starting at A, and rows on the Excel worksheet index numerically starting at 1. Therefore, I like to create helper methods to do things such as to be able to create cells using terms such as "A3" and "B4" rather than having to cast to shorts and index from 0.

If you'd like to see it, the poi-test.xls file is included here:


Page: < 1 2