【问题标题】:Export plugin on Grails 2.4.4Grails 2.4.4 上的导出插件
【发布时间】:2015-05-08 04:45:09
【问题描述】:

我想制作一个可以上传 Excel 并阅读它们的应用。并即时创建 PDF、Excel 和 Word。我使用 Grails 2.4.4。和导出 v 1.6。

这是我的代码

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 ) ]
    }

在模型上

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
    }
}

我尝试使用http://localhost:8080/myapp/authUser/list?format=csv&extension=csv

我从官方插件页面的文档中找到了解决方案

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
}

【问题讨论】:

    标签: grails plugins


    【解决方案1】:

    您可以使用 grails 导出插件将数据导出为 excel、csv、pdf 等。它易于配置和使用。

    这里是链接:http://grails.org/plugin/export

    【讨论】:

    • 我在工作中使用了该插件,但是在 2.4.4 上,它完全损坏了,我正在考虑将 java POI 用于 excel 或 itext 用于 pdf/word,如果我已经成功导出 grails。
    猜你喜欢
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2015-02-05
    • 2015-04-18
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多