Set a button:
<type= "Submit" name= "button" id= "Exprotexcel" value= "Export Excel"/>
To bind a button to an event:
$ ("#exprotExcel"). Click (function () {
window.open (".. /.. /manage/student/beadrolllistexportexcel.aspx?studenttype= "+ $ (" #selStudentType "). Val () +" &schoolcode= "+ $ (" # Selschool "). Val () +" &district= "+ $ (" #selDistrict "). Val ());
});
The Beadrolllistexportexcel.aspx page is the exported page, the page code:
<Asp:gridviewID= "GridView1"bordercolor= "Black"runat= "Server"AutoGenerateColumns= "False"font-size= "12px"Width= "530px"allowsorting= "True"onrowcreated= "gridview1_rowcreated"> <Columns> <Asp:templatefieldHeaderText= "ordinal"itemstyle-horizontalalign= "Center"> <ItemTemplate> <%# (((GridViewRow) Container). Dataitemindex+ 1) %> </ItemTemplate> </Asp:templatefield> <Asp:boundfieldDataField= "Primaryschoolname"HeaderText= "School" /> <Asp:boundfieldDataField= "TypeName"HeaderText= "category" /> <Asp:boundfieldDataField= "Name"HeaderText= "Name" /> <Asp:boundfieldDataField= "Sexcode"HeaderText= "Gender" /> <Asp:boundfieldDataField=""HeaderText= "Remarks" /> </Columns> <HeaderStyleBackColor= "Azure"font-size= "12px"horizontalalign= "Center" /> <RowStylehorizontalalign= "Center" /> <PagerStylehorizontalalign= "Center" /> </Asp:gridview>
Background code:
protected voidPage_Load (Objectsender, EventArgs e) { if(!IsPostBack) {GetList (); Export ("Application/ms-excel",string. Format ("{0}2015-Chaoyang Primary School graduate student roster. xls", DateTime.Now.ToString ("YYYY-MM-DD"))); } } Public voidGetList () {stringStudenttype = request.querystring["Studenttype"]. ToString (); stringSchoolcode = request.querystring["Schoolcode"]. ToString (); stringdistrict = request.querystring["District"]. ToString (); stringStrwhere ="1=1"; if(!string. IsNullOrEmpty (Studenttype)) {strwhere+="and typecode= '"+ Studenttype +"'"; } if(!string. IsNullOrEmpty (district)) {strwhere+="and districtcode= '"+ District +"'"; } if(!string. IsNullOrEmpty (Schoolcode)) {strwhere+="and Primaryschoolcode = '"+ Schoolcode +"'"; } DataTable DT=NewBll. Objmethod (). GetList ("view_exportexcelstudent", strwhere); Gridview1.datasource=DT; Gridview1.databind (); } /// <summary> ///define functions to export Excel/// </summary> /// <param name= "FileType" ></param> /// <param name= "FileName" ></param> Private voidExport (stringFileType,stringFileName) { for(inti =0; i < GridView1.Rows.Count; i++) {Gridview1.rows[i]. cells[4]. Attributes.Add ("style","vnd.ms-excel.numberformat:@"); } Response.Charset="GB2312"; Response.ContentEncoding=System.Text.Encoding.UTF8; Response.appendheader ("content-disposition","attachment;filename="+Httputility.urlencode (FileName, Encoding.UTF8). ToString ()); Response.ContentType=FileType; This. EnableViewState =false; StringWriter TW=NewStringWriter (); HtmlTextWriter HW=NewHtmlTextWriter (TW); Gridview1.rendercontrol (HW); Response.Write (TW. ToString ()); Response.End (); } /// <summary> ///This method must be overridden, or an error will occur/// </summary> /// <param name= "Control" ></param> Public Override voidVerifyrenderinginserverform (Control control) {}protected voidGridview1_rowcreated (Objectsender, GridViewRowEventArgs e) { if(E.row.rowtype = =datacontrolrowtype.header) {gridviewrow Rowheader=NewGridViewRow (0,0, Datacontrolrowtype.header, Datacontrolrowstate.normal);//table header rowTableheadercell cell =NewTableheadercell (); Cell. Text="2015 session of Chaoyang District Primary School graduate student roster"; Cell. ColumnSpan= at; ROWHEADER.CELLS.ADD (cell); ((GridView) sender). controls[0]. Controls.addat (0, Rowheader); } }
C # Export Excel