Skip to content

Instantly share code, notes, and snippets.

@tomgullo
Created January 4, 2013 16:10
Show Gist options
  • Save tomgullo/4453753 to your computer and use it in GitHub Desktop.
Save tomgullo/4453753 to your computer and use it in GitHub Desktop.
/*
write out data to excel spreadsheet
*/
static def writeExcel(ouputstream, headers, rows, cell_sizes) {
def workbook = Workbook.createWorkbook(outputstream)
WriteableSheet sheet = workbook.createSheet("R", 0)
for (e in cell_sizes) {
sheet.setColumnView(Integer.pareseInt(e.key), Integer.parseInt(e.value) )
}
def c = 0
headers.each() { k, v ->
sheet.addCell(new Label(c, 0, v.toString()))
if (!rows) {
sheet.setColumnView(0, 50)
sheet.addCell(new Label(0, 1, "error")
}
def r = 1
rows.each() { o ->
if (o[k] != null) {
if (o[k] instanceof java.lang.Number) {
sheet.addCell(new Number(c, r, o[k])
} else {
sheet.addCell(new Label(c, r, o[k].toString()))
}
}
r++
}
c++
}
workbook.write()
workbook.close()
}
response.setHeader("Content-disposition", "attachment; filename=test.xls")
response.contentType = "application/vnd.ms-excel"
ThisController.writeExcel(response.outputStream, ['test header':'test'],['one key':'one val'], ['0':'50'] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment