This article is about how to create a spreadsheet with the Apache POI 4.1.1 Java library. There is also a "Busy Developers' Guide" available on its home page. Mind that POI contains much more than just a spreadsheet API (MS Word, PowerPoint, ...).
Here is a screenshot of what I'm gonna present source code for:
In first line, the sheet renders the number PI 3 times in different formats. In second line it displays today's date in two different formats, and a text "End of Test". There are no formulas here, this is just about creation and formatting.
Following is the example project's Maven pom.xml, containing the library-dependency.
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fri.poi</groupId>
<artifactId>poiTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
</dependencies>
</project>
And here is the Java source for it. Explanations see below. Mind that this code needs at least Java 7.
1 | import java.io.*; |
A Workbook
is what we would call "Excel Sheet". A Sheet
is one tab inside such a workbook, having rows numbered from 0 to n, and any row can have cells numbered from 0 to n. There can be undefined rows and cells anywhere in between. Cells contain a style, by default this is just one global style, but you can also set specific styles into them. Inside the style you can have a format mask for the cell content.
Line 16 creates a Workbook
in an implementation-agnostic way by retrieving it from a factory. The boolean parameter lets me choose between
implementations. By default POI comes just with HSSF, so setting true
would cause a ClassNotFoundException
.
On line 17, I create a data-format to generate cell format masks. This seems to be globally workbook-bound.
Line 20 and 22 show how to create a new sheet with title "First Tab", and a new row in it. This row will contain 3 cells showing the number PI in different formats.
On line 24 the first cell is generated, by means of its owner row. I don't put any format on it, just the value, let's see the outcome. Mind that cell.setCellType(CellType.XXX)
is deprecated in newest POI.
Line 28 creates a cell where a format mask should round PI to 4 decimals. It is important to
Else it would use the global style that automatically is in every cell, and all cells would be formatted the same way, derived from the latest setDataFormat()
call. On line 31, the individual cell style receives its format mask from the DataFormat
object created on line 17.
Well, basically that's it.
On line 33 there is another cell showing PI, this time formatted to just 2 decimals. The subsequent row also contains 3 cells, two dates formatted in different ways, and a final string cell on line 51. Mind that the string automatically got aligned left, while numbers and dates got aligned right.
Before saving the in-memory workbook, I size its columns. There would be just "####" signs displayed in case a content is too big for its column, and that's confusing for users being new to Office tools. When using autoSizeColumn()
, this must be done after the cells received their values and formats. But on line 55, I size the columns explicitly. The width is given in 1/256-th of an average character's width. (You should encapsulate this calculation into a method!)
Finally the workbook gets closed and saved to persistence on line 62. The file ExcelExample.xlsx would be located in the working directory of the application, like given by System.getProperty("user.dir")
. The try
-clause that automatically closes the output-stream is available since Java 7.
This was just a short sneak into POI, showing basic techniques.
ɔ⃝ Fritz Ritzberger, 2019-10-28