Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Excelling in Excel with Java

Learn how to use the Jakarta POI

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Whether you have balance sheets, account information downloads, tax calculations, or pay slips, they all tend to come in Microsoft Excel. Non-IT professionals feel comfortable using Microsoft Excel as a data exchange technology. The Jakarta POI (Poor Obfuscation Implementation) API is a fantastic way for Java programmers to access Microsoft document formats. The most mature API from Jakarta POI is the HSSF (Horrible Spreadsheet Format) API, which accesses Microsoft Excel documents.

In this article, I walk you through the steps for creating and reading Excel documents, and for using fonts and cell styling—all using Java.

Note: You can download the source code for all the examples in this article from Resources.

POI terminology

The key terms associated with Jakarta POI are as follows:

  • POIFS (Poor Obfuscation Implementation File System): Java APIs for reading and writing OLE (Object Linking and Embedding) 2 compound document formats
  • HSSF (Horrible Spreadsheet Format): Java API to read Microsoft Excel
  • HDF (Horrible Document Format): Java API to read and write Microsoft Word 97
  • HPSF (Horrible Property Set Format): Java API for reading property sets using (only) Java


Create an Excel document

The Jakarta POI API can be used to create an Excel document programmatically. The important steps involved are:

  • Create a workbook: HSSFWorkbook workbook = new HSSFWorkbook();
  • Create a new worksheet in the workbook and name the worksheet "Java Excels": HSSFSheet sheet = workbook.createSheet("Java Excels");
  • Create a new row in the sheet: HSSFRow row = sheet.createRow((short)0);
  • Create a cell in the row: HSSFCell cell = row.createCell((short) 0);
  • Put some content in the cell: cell.setCellValue("Have a Cup of XL");
  • Write the workbook into the filesystem: workbook.write(fileOutputStream);


Read data from the Excel document

In this example, you'll see how to read values from an Excel document.

Let's assume this is our Excel sheet:

Employee Name Specialization Designation
Anbu Programming Senior Programmer
Jason Banking Industry Business Analyst
Ramesh Databases DBA
MackyB Accounting Delivery Head


The key steps in reading the Excel sheet are as follows:

  • Create a new Excel document reference: HSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));.
  • Refer to the sheet: By default, the first sheet in the Excel document is at reference 0: HSSFSheet sheet = workbook.getSheetAt(0);. A sheet can also be referred to by name. Let's assume that the Excel sheet has the default name "Sheet1". It can be referred to as follows: HSSFSheet sheet = workbook.getSheet("Sheet1");.
  • Refer to a row: HSSFRow row = sheet.getRow(0);.
  • Refer to a cell in the row: HSSFCell cell = row.getCell((short)0);.
  • Get the values in that cell: cell.getStringCellValue();.


A practical example

Now let's assume that we want to see the list of all declared methods and member variables in a jar file. It would be ideal to have a consolidated list of all information in one single file. We would like to view the information so that the class names are in the first column, declared fields in the second column, and declared methods in the third column, with the column headings appearing in red.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (5)
Login
Forgot your account info?

the exampleBy Anonymous on October 25, 2009, 12:32 pmWhat does this example do? I need to open it as an applet or a swing application.

Reply | Read entire comment

Read Excel In JavaBy Anonymous on May 30, 2009, 10:51 pmCheck this article on this: http://knowledge.elementfx.com/java_api/read_excel_in_java.html

Reply | Read entire comment

Problem with the above codeBy Anonymous on May 26, 2009, 5:33 amWhen I tried to use the above code in my application I got an applet alert saying - "The applet is trying to access system properties" with allow, disallow, stopapplet...

Reply | Read entire comment

All I neededBy Anonymous on March 19, 2009, 11:21 pmThis was all I needed to read Excel worksheets into Java, free my users from the aggravation of needing to create .csv files.

Reply | Read entire comment

Very helpful articleBy Anonymous on October 22, 2008, 3:35 amI found this article is very helpful. Apache POI is no doubt a great release which has made the life of a java developer easy. ============================================ All...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources