i want make app can upload excel , read them. , create pdf, excel , word on fly. use grails 2.4.4. , export v 1.6.
here's 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 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 try use http://localhost:8080/myapp/authuser/list?format=csv&extension=csv
i have found solution documentation on official plugin page
def list = { if(!params.max) params.max = 10 if(params?.exportformat && params.exportformat != "html"){ //must change exportformat cause format reserved 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 ) ] have comment or give else clause since response called before }
you use grails export plugin export data excel, csv, pdf, etc. it's easy configure , use.
here link: http://grails.org/plugin/export
Comments
Post a Comment