Use the grails export plug-in to export data (original)

Source: Internet
Author: User

Recently, I was working on a small project and met this requirement. Java poi seems to be not very useful in grails.

So I studied the export plug-in on the official website and helped me solve this problem. I would like to share my experiences with you.

 

Of course, if you do not have the grails export plug-in, you may need to install and configure it in advance:

 

Install the plug-in command: grails install-plugin Export

Then configure in the config under your project,

Add the following information to grails. Mime. types =:

 

CSV: 'text/csv ',

PDF: 'application/pdf ',

RTF: 'application/RTF ',

Excel: 'application/vnd. MS-Excel ',

ODS: 'application/vnd. Oasis. OpenDocument. spreadsheet ',

 

 

You can customize the labels displayed on the export bar by adding the following lines to grails-APP/i18n/messages. properties:

 

default.csv=CSVdefault.excel=Exceldefault.pdf=PDFdefault.rtf=RTFdefault.xml=XMLdefault.ods=ODS

 

Then, add the following tags to the page where you need data everywhere:

 

<Export: Resource/>

<Export: formats/>

Optional:

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

 

 

Then add the following to your controller:

 

1. the exported field name is based on the field name of your database and is out of order:

Def exportservice // Import Export

 

If (Params ?. Format & Params. Format! = "Html "){

Response. contenttype = configurationholder. config. grails. Mime. types [Params. Format]

Response. setheader ("conten-disposition", "attachment: filename = vaccines. $ {Params. Extension }")

Exportservice. Export (Params. format, response. outputstream, vaccine. List (Params), [:], [:])

 

 

2. Sort fields by name and order, for example:

 

 

If (Params ?. Format & Params. Format! = "Html "){

Response. contenttype = configurationholder. config. grails. Mime. types [Params. Format]

Response. setheader ("conten-disposition", "attachment: filename = vaccines. $ {Params. Extension }")

 

List fields = ["Address", "parents", "name", "dateofbirth", "sex"]

Map labels = ["Address": "Home Address", "parents": "parent name", "name": "Children's name", "dateofbirth": "Birth time ", "Sex": "gender"]

 

Def uppercase = {domain, value-> // This section looks like a check

Return Value. touppercase ()

}

Map formatters = [Address: uppercase]

Map parameters = [Parents: "King", "column. widths": [0.2, 0.3, 0.5] // The specified parameter format is fixed.

Exportservice. Export (Params. format, response. outputstream, vaccine. List (Params), fields, labels, formatters, parameters)

}

 

3. Export data based on search results, for example:

First, add

 

<Export: Resource/>

<Export: formats Params = "$ {filterparams? Filterparams: Params} "/>

 

The difference between controller and above is as follows:

Exportservice. Export (Params. format, response. outputstream, Waybill. createcriteria (). List (Params, filter), fields, labels, [:], [:])

 

The data acquisition method should be changed, so the basic is OK!

 

Well, it takes less than 20 minutes to get your data everywhere.

You have no time to export the data based on the query results. You can add the data after the system goes online.

However, I still encounter a problem: I can only export the data content on the current page and cannot export all the data.

If you have a master, give me some advice.

 

Thank you. The above are some of my experiences with using grails to help a friend develop a small software.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.