Written on
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)
}
}