如何将jqgrid过滤后的内容导出到excel?
This is exactly what I want. I want to show "export to excel" button in the pager of jqgrid, that will export the current set of data (based on the current filter).
But in Grails. Kindly suggest how to achieve it.
I was trying to do this way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JQGrid 类提供 ExportToExcel 函数,您可以使用该函数将网格内容导出到 Excel。
您可以使用 JQGridState 类在导出时维护网格的当前状态(分页、过滤、排序等之后)。您还可以指定是否只想导出当前页面或整个数据源。
The JQGrid class provides the ExportToExcel funciton which you can use to export the grid contents to excel.
You can use the JQGridState class in order to maintain the current state (after paging, filtering, sorting, etc) of the grid upon exporting. You can also specify if you want to export the current page only, all the whole datasource.
最简单的方法实际上是将数据呈现为直接的 HTML 表,然后使用内容类型告诉浏览器在 Excel 中打开它。您始终可以使用更复杂的东西(例如 Apache POI)来生成真正的电子表格,但除非您需要公式,否则实际上没有任何意义。
因此,最简单的方法就是使用视图,而不需要复杂的布局。有一个 VBScript 示例:http://support.microsoft.com/kb/271572,它具有足够的可读性,您应该不会在将其适应 Grails/GSP 时遇到太多麻烦。请注意,对于数据,一个带有表格的简单 HTML 响应似乎就足够了,而我们在实践中从来不需要嵌入的特定于 Excel 的命名空间。
您需要的 MIME 类型在这里找到答案:Setting mime type for excel document,接受答案中的标题显示如何将数据传递到 Excel 中。附件的内容配置可能是您下载数据所需的。
尽管这会下载到实际包含 HTML 的 .xls 文件,但当您打开该文件时,Excel 似乎仍然会执行正确的操作。
The easiest way of doing this is actually to render the data to a straight HTML table, and then use the content-type to tell the browser to open it in Excel. You could always use more complex stuff, like Apache POI, to generate a real spreadsheet, but unless you need formulae, there is really no point.
So the simple way to do this is simply to use a view without complex layout. There's a VBScript example at: http://support.microsoft.com/kb/271572, which is readable enough that you shouldn't have much trouble adapting it to Grails/GSP. Note that for the data, a simple HTML response with a table in it seems to be enough, the embedded Excel-specific namespace stuff we never needed in practice.
The MIME type you need is answered here: Setting mime type for excel document, and the header in the accepted answer shows you how to pass the data into Excel. The content-disposition to an attachment is probably what you need to get the data as a download.
Even though this downloads to a .xls file which actually contains HTML, Excel still appears to do the right thing when you open the file.