usingSystem.Data;usingSystem.IO;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;/// <summary>///Export Excel/// </summary> Public classgridviewexportutil{/// <summary> ///Export the GridView to Excel/// </summary> /// <param name= "FileName" >Excel file name</param> /// <param name= "GV" >GridView</param> Public Static voidExport (stringFileName, GridView GV) {HttpContext.Current.Response.Clear (); //HttpContext.Current.Response.Charset = "GB2312"; //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding ("GB2312");HttpContext.Current.Response.AddHeader ("content-disposition",string. Format ("attachment; filename={0}", FileName)); HttpContext.Current.Response.ContentType="Application/ms-excel"; HttpContext.Current.Response.Write ("<meta http-equiv=content-type content=\ "text/html; charset=utf-8\" >"); using(StringWriter SW =NewStringWriter ()) { using(HtmlTextWriter htw =NewHtmlTextWriter (SW)) { //Create A form to contain the gridTable Table =NewTable (); Table. GridLines=GV. GridLines; //table. BackColor = GV. BackColor; //Add the header row to the table if(GV. HeaderRow! =NULL) {Gridviewexportutil.preparecontrolforexport (GV). HeaderRow); Table. Rows.Add (GV. HeaderRow); Table. rows[0]. BackColor =System.Drawing.Color.Black; Table. rows[0]. ForeColor =System.Drawing.Color.White; Table. rows[0]. Height = Unit.pixel ( -); } //Add each of the data rows to the table foreach(GridViewRow rowinchGV. Rows) {gridviewexportutil.preparecontrolforexport (row); Row. Height= Unit.pixel ( A); Table. Rows.Add (row); } //Add the footer row to the table if(GV. Footerrow! =NULL) {Gridviewexportutil.preparecontrolforexport (GV). Footerrow); Table. Rows.Add (GV. Footerrow); } //render the table into the HTMLWritertable. RenderControl (HTW); //render the HTMLWriter into the responseHttpContext.Current.Response.Write (SW. ToString ()); HttpContext.Current.Response.End (); } } } Public Static voidExport (stringfileName, DataTable DT) { using(GridView GV =NewGridView ()) {GV. DataSource=DT; Gv. DataBind (); Export (FileName, GV); } } /// <summary> ///Replace any of the contained controls with literals/// </summary> /// <param name= "Control" ></param> Private Static voidPreparecontrolforexport (Control control) { for(inti =0; I < control. Controls.Count; i++) {Control current=control. Controls[i]; if(Current isLinkButton) {control. Controls.remove (current); Control. Controls.addat (i,NewLiteralControl (current asLinkButton). Text)); } Else if(Current isImageButton) {control. Controls.remove (current); Control. Controls.addat (i,NewLiteralControl (current asImageButton). AlternateText)); } Else if(Current isHyperLink) {control. Controls.remove (current); Control. Controls.addat (i,NewLiteralControl (current asHyperLink). Text)); } Else if(Current isDropDownList) {control. Controls.remove (current); Control. Controls.addat (i,NewLiteralControl (current asDropDownList). Selecteditem.text)); } Else if(Current isCheckBox) {control. Controls.remove (current); Control. Controls.addat (i,NewLiteralControl (current asCheckBox). Checked?"True":"False")); } if(current. Hascontrols ()) {gridviewexportutil.preparecontrolforexport (current); } } }}
C # Gridviewexportutil