Export Gridview to Excel Sheet in .Net C#



Namespaces:

using System.IO;
using System.Web.UI.HtmlControls;




Code:
protected void Export_Click(object sender, EventArgs e)
    {
        string attachment = "attachment; filename=Export.xls";

        Response.ClearContent();

        Response.AddHeader("content-disposition", attachment);

        Response.ContentType = "application/ms-excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        // Create a form to contain the grid

        HtmlForm frm = new HtmlForm();


        gridview1.Parent.Controls.Add(frm);

        frm.Attributes["runat"] = "server";

        frm.Controls.Add(gridview1);
        frm.RenderControl(htw);
        Response.Write(sw.ToString());

        Response.End();
 
    }
Export Gridview to Excel Sheet in .Net C# Export Gridview to Excel Sheet in .Net C# Reviewed by NEERAJ SRIVASTAVA on 11:33:00 AM Rating: 5

No comments:

Powered by Blogger.