Wednesday, August 7, 2013

How to Export Gridview Data to Word in Asp.net

Introduction: In this article I will explain how we can Export the Gridview Data to Word in Asp.net
Description:
Take a new website. Add a webform to website and design .aspx page as shown below:
<body>
    <form id="form1" runat="server">
    <div>
     <table>
    <tr><td>&nbsp;</td><td align="right">
        <asp:Button ID="Button1" runat="server" Text="Export To MS-WORD"
            onclick="Button1_Click" /></td></tr>
    <tr><td>&nbsp;</td><td> <asp:GridView ID="grdstudent" runat="server" AutoGenerateColumns="false" DataKeyNames="STUDENT_ID" DataSourceID="SqlDataSource1">
        <Columns>
        <asp:BoundField DataField="STUDENT_NAME" HeaderText="STUDENT NAME" />
        <asp:BoundField DataField="STUDENT_ADDRESS" HeaderText="STUDENT ADDRESS" />
        <asp:BoundField DataField="STUDENT_CLASS" HeaderText="STUDENT CLASS" />
        </Columns>
        </asp:GridView></td></tr>
    </table>
      
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:TestBlogConnectionString %>"
            SelectCommand="SELECT * FROM [STUDENT_DETAIL]"></asp:SqlDataSource>
    </div>
    </form>
</body>

Now go to .aspx.cs page and write the below given code:
public override void VerifyRenderingInServerForm(Control control)
    {
        // Verifies that the control is rendered
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StudentDetail's.doc"));
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-word ";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grdstudent.AllowPaging = false;
            grdstudent.DataBind();
            grdstudent.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }

In VB (.aspx.vb)
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Verifies that the control is rendered
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Response.Clear()
            Response.Buffer = True
            Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "Customers.doc"))
            Response.Charset = ""
            Response.ContentType = "application/vnd.ms-word "
            Dim sw As New System.IO.StringWriter()
            Dim htw As New HtmlTextWriter(sw)
            grdstudent.AllowPaging = False
            grdstudent.DataBind()
            grdstudent.RenderControl(htw)
            Response.Output.Write(sw.ToString())
            Response.Flush()
            Response.[End]()
        Catch ex As Exception
        End Try

    End Sub


If you get any error "Control 'grdstudent' of type 'GridView' must be placed inside a form tag with runat=server" read this article:


http://articlemirror.blogspot.in/2013/07/control-grdstudent-of-type-gridview.html

http://articlemirror.blogspot.in/2013/07/a-page-can-have-only-one-server-side.html
Run the project and check result.

Is it helpful?


If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment