I have a list with information and I want to export it to Excel. How do I do it?
Is the "Export Plugin" any good? I think I saw one a while ago to export files to Excel but I can't find it anymore.
I have a list with information and I want to export it to Excel. How do I do it?
Is the "Export Plugin" any good? I think I saw one a while ago to export files to Excel but I can't find it anymore.
A quick search for excel in the plugins portal found this plugin. I didn't work with it so I can't comment about it's quality.
If you want actual Excel documents (rather than just CSV files), I've used the JExcel library with some success. Here's a quickly-written example that could probably be Groovy-fied a little bit.
Edit: Updated my example to do this in a controller. Architecturally it would make more sense to split this up a bit, but this is just for example's sake.
import jxl.*
import jxl.write.*
class SomeController {
def report = {
def file = createReport(MyDomain.list())
response.setHeader('Content-disposition', 'attachment;filename=Report.xls')
response.setHeader('Content-length', "${file.size()}")
OutputStream out = new BufferedOutputStream(response.outputStream)
try {
out.write(file.bytes)
} finally {
out.close()
return false
}
}
private File createReport(def list) {
WorkbookSettings workbookSettings = new WorkbookSettings()
workbookSettings.locale = Locale.default
def file = File.createTempFile('myExcelDocument', '.xls')
file.deleteOnExit()
WritableWorkbook workbook = Workbook.createWorkbook(file, workbookSettings)
WritableFont font = new WritableFont(WritableFont.ARIAL, 12)
WritableCellFormat format = new WritableCellFormat(font)
def row = 0
WritableSheet sheet = workbook.createSheet('MySheet', 0)
list.each {
// if list contains objects with 'foo' and 'bar' properties, this will
// output one row per list item, with column A containing foo and column
// B containing bar
sheet.addCell(new Label(0, row, it.foo, format))
sheet.addCell(new Label(1, row++, it.bar, format))
}
}
}
Using this library lets you do things like formatting, using multiple worksheets, etc.