如何在java struts2中实现导出到excel的功能 [英] How to implement export to excel functionality in java struts2

查看:229
本文介绍了如何在java struts2中实现导出到excel的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用了struts2 jQuery插件。

我使用网格来显示项目。

如何将网格中存在的项目导出到microsoft excel,点击一个按钮?

解决方案

将jqGrid显示的列表发布到Action(让我们说ExcelExportAction)。



在execute()方法中,使用 JExcel Apache POI



使用列表中的数据填充Excel。 Google的这个例子就是这个例子。



通过Action(及其Getter)的InputStream显示Excel的结果。



返回SUCCESS。



将Action的成功结果映射为流结果类型,并使用excel内容类型,如下所示:

 < action name =excelExportclass =your.package.ExcelExportAction> 
< result name =successtype =stream>
< param name =contentType> application / vnd.ms-excel< / param>
< param name =contentDisposition>
附件;文件名= MyExcelName.xls < / PARAM>
< param name =bufferSize> 1024< / param>
< / result>
< / action>


I am using struts2 jQuery plugin in my project.
I use grid to display items..
How to export the items present in the grid to microsoft excel on click of a button?

解决方案

Post the List displayed by the jqGrid to an Action (let's say ExcelExportAction).

In the execute() method, create an Excel using JExcel or Apache POI.

Populate the Excel with the data from the List. Google it, it's full of examples out there.

Expose the result of the Excel through an InputStream from the Action (and its Getter).

Return SUCCESS.

Map the Success Result of the Action as Stream result type and with the excel contenttype like follow:

<action name="excelExport" class="your.package.ExcelExportAction">
    <result name="success" type="stream">
            <param name="contentType">application/vnd.ms-excel</param>
            <param name="contentDisposition">
                           attachment; filename="MyExcelName.xls"</param>
            <param name="bufferSize">1024</param>           
    </result>
</action>

这篇关于如何在java struts2中实现导出到excel的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆