Grails - Export Plugin

Export Plugin

Author: Andreas Schmitt

Abstract

This plugin allows you to export domain objects to a variety of formats. Currently supported are CSV, Excel, ODS (Open Document Spreadsheets), PDF and XML. It can be easily extended to support further formats.

Installation

To install the plugin type:> grails install-plugin export

Add MimeTypes for CSV, Excel, ODS and PDF to grails-app/conf/Config.groovy so that it looks similar to:

grails.mime.types = [ html: ['text/html','application/xhtml+xml'],
                      xml: ['text/xml', 'application/xml'],
                      text: 'text-plain',
                      js: 'text/javascript',
                      rss: 'application/rss+xml',
                      atom: 'application/atom+xml',
                      css: 'text/css',
                      csv: 'text/csv',
                      pdf: 'application/pdf',
                      excel: 'application/vnd.ms-excel',
                      ods: 'application/vnd.oasis.opendocument.spreadsheet',
                      all: '*/*',
                      json: ['application/json','text/json'],
                      form: 'application/x-www-form-urlencoded',
                      multipartForm: 'multipart/form-data'
                    ]

Usage

To use the export features add the following markup to your GSP header:

<export:resource />

This will include the necessary CSS files for the export bar. The icons used for the export bar are from Mark James

http://www.famfamfam.com/lab/icons/silk/.

Add the export bar to your GSP page e.g. below the paginate buttons:

…
<div class="paginateButtons">
    <g:paginate total="${Book.count()}" />
</div>

<export:formats />

or

<export:formats formats="['csv', 'excel', 'ods', 'pdf', 'xml']" />

</div> ...

The formats tag supports the following attributes and allows you to pass through HTML attributes:

  • formats (Formats which should be displayed, List of Strings, e.g. ['csv', 'excel', 'ods', 'pdf', 'xml'])
  • params (Additional request parameters, Map, e.g. [sort: params?.sort, order: params?.order])
  • action (Action which should be called, defaults to current action)
  • controller (Controller which should be called, defaults to current controller)
You can customize the labels displayed on the export bar by adding the following lines to grails-app/i18n/messages.properties:

default.csv=CSV
default.excel=Excel
default.pdf=PDF
default.xml=XML
default.ods=ODS

To make the plugin working you need to add some code to your controller:

import org.codehaus.groovy.grails.commons.ConfigurationHolder

class BookController {

// Export service provided by Export plugin def exportService

...

def list = { if(!params.max) params.max = 10

if(params?.format && params.format != "html"){ response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] response.setHeader("Content-disposition", "attachment; filename=books.${params.format}")

exportService.export(params.format, response.outputStream,Book.list(params), [:], [:]) }

[ bookInstanceList: Book.list( params ) ] }

} ...

This will export all book objects with all attributes except version. You can also configure which attributes should be exported. The following example will rely on a simple domain class:

class Book {

String title String author }

And now for the controller code:

import org.codehaus.groovy.grails.commons.ConfigurationHolder
class BookController {

def exportService

...

def list = { if(!params.max) params.max = 10

if(params?.format && params.format != "html"){ response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] response.setHeader("Content-disposition", "attachment; filename=books.${params.format}")

List fields = ["author", "title"] Map labels = ["author": "Author", "title": "Title"]

// Formatter closure def upperCase = { value -> return value.toUpperCase() }

Map formatters = [author: upperCase] Map parameters = [title: "Cool books"]

exportService.export(params.format, response.outputStream, Book.list(params), fields, labels, formatters, parameters) }

[ bookInstanceList: Book.list( params ) ] }

} ...

You can see how the fields are now set explicitly. You can use labels to customize the output of the fields e.g. for i18n. Using formatters it is possible to customize how the resulting values are generated which can be used to specify a particular date format etc. by just mapping an attribute name to a closure.

Plugin version history ^

  • Feb. 15, 2009 (version 0.1)
    • Initial version released
    • Supported formats
      • CSV
      • Excel
      • ODS
      • PDF
      • XML
USER COMMENTS:
  • blackrosezy : Need support for nested header
  • feature request: aggregate/synthesized columns. This could be accomplished easily by passing in the entire row to the formatter closure.