Friday 15 March 2013

Export gridview to word using asp.net

Export to Word

To export to word write this code for the btnexporttoword Click event.

             Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.word";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        // Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        gv.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(gv);
        frm.RenderControl(htmlWrite);
        //GridView1.RenderControl(htw);
        Response.Write(stringWrite.ToString());
        Response.End();

No comments:

Post a Comment