第一次尝试导出 excel 时出错

Error exporting excel on first attempt

 def exportExcel(){
        if(!params.max) params.max = 10

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

            response.contentType = grailsApplication.config.grails.mime.types[params.type]
            response.setHeader("Content-disposition", "attachment; filename=agents.${params.extension}")

            List fields = ["orgTypeId", "name"]
            Map labels = ["orgTypeId":"DP Id", "name":"Name"]

            def upperCase = { domain, value ->
                return value.toUpperCase()
            }

            Map parameters = [title: "Agent List", "column.widths":[0.15, 0.4]]
            Map formatters = [name: upperCase]

            exportService.export(params.type, response.outputStream, Agent.list(params), fields, labels, formatters, parameters)
        }

        render(view: 'index',model: [organizationTypeCount: Agent.count(), organizationType: Agent.list(params)])

    }

这是我要导出的代码 excel。当我点击导出按钮时。它显示失败网络错误。 如果我恢复下载,它将被下载。

这是错误:java.lang.IllegalStateException: getOutputStream() has already been called for this response

请帮我解决这个问题。

不能将附件数据写入输出流,然后渲染索引视图;它试图将您的视图渲染到您已将附件发送到的同一输出流。如果删除呈现索引视图的行,您的代码应按预期工作。

如果您需要生成一个 attachment/download 并移动到浏览器中的另一个视图,您将需要发送两个请求来执行此操作。