Extjs-grid Data Export to Excel
Recently, due to project issues, you need to resolve the issue of EXTJS Export to Excel.
Here is a brief description of the steps to solve the problem:
1, first in the JS file to write a button of the handler event, by clicking the button to implement the call Exportexcel (Gridpanel,{store:null, title: "})
The source code for the Exportexcel method is as follows:
/** Execl Export method **/function Exportexcel (gridpanel, config) {if(gridpanel) {varTmpstore =Gridpanel.getstore ();varTmpexportcontent ="'; //The following processing paging grid data export problem, from the server to get all the data, you need to consider the performance varTmpparam = Ext.ux.clone (tmpstore.lastoptions);//The parameter information of the original grid data source is cloned here if(Tmpparam && Tmpparam.params) {Delete (tmpparam.params[TmpStore.paramNames.start]);//Delete Paging ParametersDelete (Tmpparam.params[TmpStore.paramNames.limit]); } varTmpallstore =NewExt.data.GroupingStore ({//redefining a data source proxy:tmpStore.proxy, Reader:tmpStore.reader}); Tmpallstore.on ('Load', function (store) {Config.store=store; Tmpexportcontent= Gridpanel.getexcelxml (false, config);//This method uses one of the extensions if(Ext.isie | | Ext.issafari | | Ext.issafari2 | | EXT.ISSAFARI3) {//in these browsers, the IE8 test cannot be downloaded directly. if(! Ext.fly ('Frmdummy')) { varfrm = Document.createelement ('form'); Frm.id='Frmdummy'; Frm.name=ID; Frm.classname='X-hidden'; Document.body.appendChild (frm); } Ext.Ajax.request ({//send the generated XML to the server side, paying special attention to the address of this pageUrl:'gridtoexcel.aspx', Method:'POST', Form:Ext.fly ('Frmdummy'), Callback:function (O, S, r) {//alert (r.responsetext); }, Isupload:true, params: {exportcontent:tmpexportcontent, ExportFile:gridPanel.id +'. xls' } }); } Else{document.location='Data:application/vnd.ms-excel;base64,'+Base64.encode (tmpexportcontent); } }); Tmpallstore.load (Tmpparam); //Get all data }};
Also need to refer to the above JS file in the HTML file/aspx file to refer to a gridtoexecl.js file, because the code is too many, so I uploaded it, do not post code.
Gridtoexecl.js
2, gridtoexcel.aspx page of the relevant code:
Front desk:
<%@ page language="C #" autoeventwireup="true" codefile= " GridToExecl.aspx.cs " inherits="gridtoexecl" validaterequest="false" // validaterequest = "false" is very important, missing it, IE series browser does not export properly
Background:
Public Partial classgridtoexcel:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { if(!IsPostBack) { if(request["exportcontent"] !="") { stringTmpfilename ="Excel.xls"; stringTmpcontent = request["exportcontent"];//get the contents of the file delivered if(request["Exportfile"] !="") {Tmpfilename= request["Exportfile"];//gets the file name that is passed upTmpfilename = System.Web.HttpUtility.UrlEncode (Request.ContentEncoding.GetBytes (tmpfilename));//handling the Chinese file name situation } Response.Write ("&lt;script&gt;document.close ();&lt;/script&gt;"); Response.Clear (); Response.Buffer=true; Response.ContentType="Application/vnd.ms-excel"; Response.AddHeader ("content-disposition","attachment;filename=\ ""+ Tmpfilename +"\""); Response.Charset=""; This. EnableViewState =false; System.IO.StringWriter TMPSW=NewSystem.IO.StringWriter (); System.Web.UI.HtmlTextWriter TMPHTW=NewSystem.Web.UI.HtmlTextWriter (TMPSW); Tmphtw.writeline (tmpcontent); Response.Write (Tmpsw.tostring ()); Response.End (); } } }