Blog JVM Stuff.

GSheets - A Groovy Builder based on Apache POI

I recently asked on Twitter whether there was an Apache POI [0] Groovy builder for generating Excel workbooks. I did find a few blog posts [1], [2], but none of the provided builders seemed to offer the ability to write styled and formatted Excel workbooks with possibly multiple sheets. So I decided to setup a new Github project (what else?) named GSheets [3]. At the current point of time it consists of a single class called ExcelFile, a Groovy builder for Excel workbooks based on HSSF [4].
Workbook workbook = new ExcelFile().workbook {

            // section for workbook formatting styles
            styles {
                font("bold")  { Font font ->
                    font.setBoldweight(Font.BOLDWEIGHT_BOLD)
                }

                cellStyle ("header")  { CellStyle cellStyle ->
                    cellStyle.setAlignment(CellStyle.ALIGN_CENTER)

                }
            }

	    // section for workbook data
            data {
                sheet ("Export")  {
                    header(["Column1", "Column2", "Column3"])

                    row(["a", "b", "c"])
                }
            }

            // section for applying commands i.e. apply a cell style, merge cells etc.
            commands {
                applyCellStyle(cellStyle: "header", font: "bold", rows: 1, columns: 1..3)
            }
        }
As can be seen in the code sample above, ExcelFile gives a simple DSL for specifying styles, table data and commands. The styles section is used to define CellStyle objects. The data section specifies all the (header) rows and the commands section is used to apply styles, merge cells, and do various other operations on the current workbook sheet.

What's next?

I plan to continuously extend ExcelFile with all the spreadsheet generating features HSSF provides. If you want to contribute, feel free to ping me on Twitter (@asteingr) or this blog.

[0] Apache POI - http://poi.apache.org/
[1] http://skepticalhumorist.blogspot.com/2010/12/groovy-dslbuilders-poi-spreadsheets.html
[2] http://www.technipelago.se/content/technipelago/blog/44
[3] GSheets Github - https://github.com/andresteingress/gsheets
[4] Apache POI Spreadsheets - http://poi.apache.org/spreadsheet/