How to implement export to excel functionality in java struts2

青春壹個敷衍的年華 提交于 2020-01-25 08:04:27

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/13873088/how-to-implement-export-to-excel-functionality-in-java-struts2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!