grails - 第一次尝试导出excel时出错
问题描述
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
请帮我解决这个问题。
解决方案
您不能将附件数据写入输出流,然后渲染索引视图;这是试图将您的视图呈现到您已将附件发送到的相同输出流。如果您删除渲染索引视图的行,您的代码应该可以按预期工作。
如果您需要生成附件/下载并移动到浏览器中的另一个视图,则需要发送两个请求来执行此操作。
推荐阅读
- fortran - 适用于 Linux 的 Windows 子系统上的 GNU Fortran 编译器 - 将内部函数传递给另一个过程时出现分段错误
- docker - Dockerfile:复制zip并打开它
- javascript - 遍历数组和其中的平均值/求和值
- java - ViewModel 没有零参数构造函数 hilt Java
- swift - Swift init 方法中的三个点
- javascript - 浏览器/节点 js 中的睡眠功能
- c - 我想知道这个程序输出背后的原因
- python - 如何在不破坏环境(例如 pip)的情况下将 Raspberry Pi Zero W 从 Python 3.7 降级到 3.6,以使用 pyaudio
- android - 从通知阴影更改主题时的非空参数
- c - 要求输入全部而不是单独输入