C # Gridviewexportutil

Source: Internet
Author: User

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

Related Article

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.