Exporting a plugin to Grails 2.4.4

I want to create an application that can download Excel and read them. and create PDF, Excel and Word on the fly. I am using Grails 2.4.4. and Export v 1.6.

Here's my code

class AuthUserController {
    def exportService
    def grailsApplication
    ....
    def list = {
        if(!params.max) params.max = 10

        if(params?.format && params.format != "html"){
            response.contentType = grailsApplication.config.grails.mime.types[params.format]
            response.setHeader("Content-disposition", "attachment; filename=AuthUser.${params.extension}")
            List fields = ["username", "email"]
            Map labels = ["username": "username", "email": "email"]

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

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

            Map formatters = [username: upperCase]
            Map parameters = [username: "admin", "column.widths": [0.2, 0.3, 0.5]]
            exportService.export(params.format, response.outputStream,  response.outputStream,AuthUser.list(params), [:], fields, labels, formatters, parameters)
        }

        [ authUserInstanceList: AuthUser.list( params ) ]
    }

      

and on the model

class AuthUser {

    transient springSecurityService

    String username
    String password
    String email
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    static transients = ['springSecurityService']

    static hasMany = [madeBillings:Billing, goodsBillings:GoodsBill, approvedRequest:TenantRequest, journals:Journal]

    static constraints = {
        username blank: false, unique: true
        password blank: false
        email blank: false, unique: true, email: true
    }

    static mapping = {
        password column: '`password`'
        tablePerHierarchy false
    }

    Set<AuthRole> getAuthorities() {
        AuthUserAuthRole.findAllByAuthUser(this).collect { it.authRole }
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
    }

    String toString() {
        username
    }
}

      

I am trying to use http: // localhost: 8080 / myapp / authUser / list? Format = csv & extension = csv

I found the solution from the documentation on the plugin official page

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

    if(params?.exportFormat && params.exportFormat != "html"){ //must change to exportFormat cause format is reserved for the default grails format
        response.contentType = grailsApplication.config.grails.mime.types[params.exportFormat]
        response.setHeader("Content-disposition", "attachment; filename=AuthUser.${params.extension}")

        exportService.export(params.exportFormat, response.outputStream,AuthUser.list(params), [:], [:])
    }

    //   [ authUserInstanceList: AuthUser.list( params ) ] you have to comment this or give an else clause since  the response already called before
}

      

+3


source to share


1 answer


You can use the Grails export plugin to export data to excel, csv, pdf, etc. It's easy to set up and use.



Here is the link: http://grails.org/plugin/export

+1


source







All Articles