GridView: Export GridView Rows To Excel .



Below code is used to export row from Gridview to Excel.

private void ExportToExcel(GridView gv)
        {
            Response.Clear();

            Response.AddHeader("content-disposition", "attachment; filename=NewExcel.xls");

            Response.Charset = "";

            Response.ContentType = "application/vnd.xls";

            System.IO.StringWriter stringWrite = new System.IO.StringWriter();

            System.Web.UI.HtmlTextWriter htmlWrite =new HtmlTextWriter(stringWrite);

            gv.RenderControl(htmlWrite);

            Response.Write(stringWrite.ToString());

            Response.End();

        }

No comments: