ExtJS Exporting Excel

Source: Internet
Author: User

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 ("&amp;lt;script&amp;gt;document.close ();&amp;lt;/script&amp;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 (); }        }    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.