CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to Export GridView Control to Word document in ASP.Net ?
Submitted By Satheesh Babu B
On 7/7/2010 7:21:29 AM
Tags: ASP.Net,C#,CodeDigest  

How to Export GridView Control to Microsoft Word document(.doc) in ASP.Net ?

 

This little code snippet will help us to export the GridView content to Microsoft word document in ASP.Net.


CodeBehind
protected void Page_Load(object sender, EventArgs e)
    {
      gvEmployee.DataSource = GetData();                    
      gvEmployee.DataBind();

    }
 protected void btnExpWord_Click(object sender, EventArgs e)
    {
        string attachment = "attachment; filename=Employee.doc";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/vnd.ms-word";
        StringWriter stw = new StringWriter();
        HtmlTextWriter htextw = new HtmlTextWriter(stw);
        gvEmployee.RenderControl(htextw);
        Response.Write(stw.ToString());
        Response.End();
    }
 public override void VerifyRenderingInServerForm(Control control)
    {

    }

 

ASPX
 <asp:GridView ID="gvEmployee" runat="server">
 </asp:GridView>
 <asp:Button ID="btnExpWord" runat="server" Text="Export to word"
                        onclick="btnExpWord_Click" />

 

Read the below article to export GridView to Excel and PDF,

Export to PDF file in ASP.Net-Gridview to PDF, ASPX Page Content to PDF

Export to Excel in ASP.Net 2.0 - Gridview to Excel, DataTable to Excel

 

Happy Coding!!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..