Reputation: 2329
gsp is divided into two, the top part is like a search filter, and the bottom part is the regular list that is provided by Grails
I have a template _list.gsp in my list.gsp. And I wanna create a button to make that _list.gsp template a MS Excel file.
But I want only the template to be on the Excel file. Not the rest of the page
How can I do this in the simplest and direct way possible?? Thank you!
Upvotes: 0
Views: 2131
Reputation: 114
Take a look at the Excel-export plugin. This has support for downloading only in .xlsx format. Exports an excel with the simplest coding. I switched to this plugin from export plugin since had a clash with rendering plugin. https://grails.org/plugin/excel-export
response.contentType = 'application/vnd.ms-excel'
response.setHeader("Content-disposition", "attachment; filename=OrderList.xlsx")
new WebXlsxExporter().with { //pass the contructor with "templateFileNameWithPath"
setResponseHeaders(response,new Date().format('dd-MM-yyyy_hh-mm')+"OrderList.xlsx")
fillHeader(labels)
add(data, fields)
save(response.outputStream)
}
response.outputStream.flush()
response.outputStream.close()}
Upvotes: 0
Reputation: 2130
All you need is to send search data to your controller, in the controller you can send content as explained by mschonaker, this is an example of what I did
def report = {
def reportType = params.report
....
.....
//Service class that collects data based on search data passed in params
def data = reportservice.execute(param)
//called export plugin to generate xls file
response.contentType = ConfigurationHolder.config.grails.mime.types[params.format]
response.setHeader("Content-disposition", "attachment; filename=${fileName}")
exportService.export(...)
}
Upvotes: 0
Reputation: 7305
Try this quite dirty trick in your index method:
response.setHeader( 'Content-Disposition', 'attachment;filename=list.xls' )
Explanation: the trick is that an HTML page renamed as *.xls can be read by office software suites. What you're doing is telling the browser to download a file with that name, instead of navigating to a regular HTML file. If you want to use this in a button, you should proceed as with any HTML generating page, and then add this header to the response. response
is global to actions in Grails controllers.
Upvotes: 1
Reputation: 33345
you can also take a look at the export plugin if you want to export the list to excel format
http://www.grails.org/plugin/export
Upvotes: 1