Runtime Generating Ms Word Document in ASP.NET C# VB.Net

Generating report from web application is one of the most common task. For reporting purpose we can use many third party DLL. But we can easily produce a report based on HTML content & render those content in a MS Word document. Though it’s not a pure MS Word document but we can meet the client requirement in this way easily. Here in this article I will explain “How we can Runtime Generate Ms Word Document in ASP.NET C# VB.Net“. To do that I am using StringBuilder class which is very efficient for string manipulation. By using StringBuilder class I will create a runtime HTML document & render the response as “application/msword” mime type. That’s it.

Runtime Generating Ms Word Document in ASP.NET C# VB.Net

To do the Example Prepare Some HTML Markup like below:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<b>Your Name:</b><br />
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox></br>
<b>Your Age:</b><br />
    <asp:TextBox ID="txtAge" runat="server"></asp:TextBox></br>
<b>Monthly Income:</b><br />
    <asp:TextBox ID="txtIncome" runat="server"></asp:TextBox></br>
    <asp:Button ID="Button1" runat="server" Text="Generate MS Word Document" 
        onclick="Button1_Click" />

Now Runtime Generate HTML Document from Code Behind:
C# Code:

    protected void Button1_Click(object sender, EventArgs e)
        StringBuilder str = new StringBuilder();

        str.Append("<tr><td align='center' colspan='2'><h1>Income Statement - "+DateTime.Now.Year+"</h1></td></tr>");
        str.Append("<tr><td align='center'>January</td><td align='center'>"+"US$ "+txtIncome.Text+"</td></tr>");
        str.Append("<tr><td align='center'>February</td><td align='center'>" + "US$ " + txtIncome.Text + "</td></tr>");
        str.Append("<tr><td align='center'>March</td><td align='center'>" + "US$ " + txtIncome.Text + "</td></tr>");
        str.Append("<tr><td align='center'>April</td><td align='center'>" + "US$ " + txtIncome.Text + "</td></tr>");
        str.Append("<tr><td align='center'>May</td><td align='center'>" + "US$ " + txtIncome.Text + "</td></tr>");
        str.Append("<tr><td align='center'>June</td><td align='center'>" + "US$ " + txtIncome.Text + "</td></tr>");
        str.Append("<tr><td align='center'>July</td><td align='center'>" + "US$ " + txtIncome.Text + "</td></tr>");

        string fileName = "MsWordSample.doc";
        Response.AppendHeader("Content-Type", "application/msword");
        Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);

Note: Don’t forget to add “System.Text” namespace.

VB.Net Code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Dim str As New StringBuilder()

        str.Append("<tr><td align='center' colspan='2'><h1>Income Statement - " + DateTime.Now.Year.ToString() & "</h1></td></tr>")
        str.Append(("<tr><td align='center'>January</td><td align='center'>" & "US$ ") + txtIncome.Text & "</td></tr>")
        str.Append(("<tr><td align='center'>February</td><td align='center'>" & "US$ ") + txtIncome.Text & "</td></tr>")
        str.Append(("<tr><td align='center'>March</td><td align='center'>" & "US$ ") + txtIncome.Text & "</td></tr>")
        str.Append(("<tr><td align='center'>April</td><td align='center'>" & "US$ ") + txtIncome.Text & "</td></tr>")
        str.Append(("<tr><td align='center'>May</td><td align='center'>" & "US$ ") + txtIncome.Text & "</td></tr>")
        str.Append(("<tr><td align='center'>June</td><td align='center'>" & "US$ ") + txtIncome.Text & "</td></tr>")
        str.Append(("<tr><td align='center'>July</td><td align='center'>" & "US$ ") + txtIncome.Text & "</td></tr>")

        Dim fileName As String = "MsWordSample.doc"
        Response.AppendHeader("Content-Type", "application/msword")
        Response.AppendHeader("Content-disposition", "attachment; filename=" & fileName)
    End Sub

Note: Don’t forget to add “System.Text” namespace.

Hope now you can generate MS Word report from your Asp.Net C# or VB.Net web application.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *