grails 导出‘csv', 'excel', 'ods', 'pdf', 'rtf', 'xml',用plusin

阅读更多

*问题:grails 导出‘csv', 'excel', 'ods', 'pdf', 'rtf', 'xml',用plusin、简单

*思想:天下事有难易乎?为之,则难者亦易矣;不为,则易者亦难矣。】

    任何事情请先尝试从官网学习,确实不行再看论坛,希望大家都分享学习方法及享受其中的乐趣。

    export grails plusinhttp://grails.org/plugin/export

 

*目录:

    1~6   很简单的实现导出domain里面的内容

7 自定义导出内容,表头可以国际化,字段可以特殊处理,比如取字典、转格式...

 

*Integrated Develop Environment:   ItelliJ

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

grails 导出‘csv', 'excel', 'ods', 'pdf', 'rtf', 'xml',用plusin_第1张图片


grails 导出‘csv', 'excel', 'ods', 'pdf', 'rtf', 'xml',用plusin_第2张图片

2Add MimeTypes for CSV, Excel, ODS, PDF and RTF 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',

                      rtf: 'application/rtf',

                      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'

                    ]

3Export plugin uses resource plugin to provide resources (since version 1.1). Add the following markup to your GSP header:

"export"/>

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

"paginateButtons">

    "${Book.count()}" />

or

里需要集中导出就配置几种; params="${params}" 带查询条件 -->

"['csv', 'excel', 'ods', 'pdf', 'rtf', 'xml']"  params="${params}"/>

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', 'rtf', '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)

5You 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.rtf=RTF

default.xml=XML

default.ods=ODS

修改显示内容:

        默认: 

        修改:

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

class BookController {

    // Export service provided by Export plugin     

    def exportService

    def grailsApplication  //inject 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=books.${params.extension}")

//Book.list(params)  列表修改为自己定义的列表

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

               }

        [ bookInstanceList: Book.list( params ) ]

    }

}

...

体会:神马,简单6步实现了导出poi得气绝了,犒劳犒劳自己 

7、自定义导出内容

    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:

class BookController {

    //注入 导入service

    def exportService

    //应用对象 猜想为读配置文件信息

    def grailsApplication  //inject GrailsApplication

    def list = {

        //导出最大记录数设置 设置大一点,本人设置1k

        if(!params.max) params.max = 1000

                //根据params.format参数来判断是否做导出操作

                               if(params?.format && params.format != "html"){

                                              response.contentType = grailsApplication.config.grails.mime.types[params.format]                        

                        ////设置HTML header内容,1、为附件下载形式 2、文件名字filename

                                              response.setHeader("Content-disposition", "attachment; filename=books.${params.extension}")

                        //定制导出的 domain.fields

                                              List fields = ["author", "title"]

                        //定制导出的表头

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

                        /* 之前版本的写法 Formatter closure in previous releases

                                              def upperCase = { value ->

                                                             return value.toUpperCase()

                                              }            */

                        // 新版本的写法 格式化字段 Formatter closure

                                              def upperCase = { domain, value ->

                                                             return value.toUpperCase()

                                              }

                                              //配置格式化给指定field

                                                Map formatters = [author: upperCase]          

                                              //配置表格的标题,默认为"sheet1",设置单元格的比例

                                              Map parameters = [title: "Cool books", "column.widths": [0.2, 0.3, 0.5]]

                        //缺省项 "[:]"

                                              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. It is also possible to specify nonexistant fields and add formatters for those fields to produce static content like today's date.

 

  • grails 导出‘csv', 'excel', 'ods', 'pdf', 'rtf', 'xml',用plusin_第3张图片
  • 大小: 2.6 KB
  • grails 导出‘csv', 'excel', 'ods', 'pdf', 'rtf', 'xml',用plusin_第4张图片
  • 大小: 3.5 KB
  • grails 导出‘csv', 'excel', 'ods', 'pdf', 'rtf', 'xml',用plusin_第5张图片
  • 大小: 52 KB
  • grails 导出‘csv', 'excel', 'ods', 'pdf', 'rtf', 'xml',用plusin_第6张图片
  • 大小: 19.2 KB
  • 查看图片附件

你可能感兴趣的:(grails,导出‘csv','excel','ods','pdf','rtf','xml',grails用plusin,grails,groovy)