To Save or Open the Data of DataGrid into Excel sheet using C#.net

Write down the code in Save button click event.

note - GridAllRecord is the name of DataGrid which is to be saved.

        Response.Clear();
        Response.Buffer = true;
        Response.ContentType="application/vnd.ms-excel";
        Response.Charset = "";
        this.EnableViewState = false;
        System.IO.StringWriter  oStringWriter = new      System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        GridAllRecord.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();