Generate Word documents from ASP.NET

Overview

Ever needed to generate Word documents from your ASP.NET applications? There are a couple of components which will help to generate Word documents, but using these components may have some overhead such as Component Registration, setting permissions, licenses, etc., Sometimes, it may even become difficult to understand their features and use them. Generating word document in ASP.NET with HTML is incredibly easy.

The following sample demonstrates how to place Heading and Table dynamically in the word document in ASP.NET web applications using HTML.

Requirements

Microsoft Visual Web Developer 2005+ Express Edition

Create the Web Application project

Add the new Web Site Application project (with name as Generate Word Document) and place the button (with name as btnGenerateDocument) in the Default.aspx web form as shown below.

Figure 1

Double click the highlighted code behind file from the Solution Explorer and add the following namespaces.

Listing 1

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

Finally, paste the following code in the button click event

Listing 2

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset ="";

HttpContext.Current.Response.ContentType ="application/msword";

string strFileName = "GenerateDocument"+ ".doc";
HttpContext.Current.Response.AddHeader("Content-Disposition",
  "inline;filename=" + strFileName);

StringBuilder strHTMLContent = newStringBuilder();

strHTMLContent.Append(
  " <h1 title='Heading' align='Center'
  style='font-family: verdana; font -size: 80 % ;
 color: black'><u>Document Heading</u> <  / h1 > ".ToString());

strHTMLContent.Append("<br>".ToString()); strHTMLContent.Append(
  "<table align='Center'>".ToString());

// Row with Column headers
strHTMLContent.Append("<tr>".ToString()); strHTMLContent.Append(
  "<td style='width:100px; background:
# 99CC00'><b>Column
1 <  / b >  <  / td >".ToString());

strHTMLContent.Append("<td style='width:100px;background:
# 99CC00'><b>Column
2 <  / b >  <  / td >".ToString());

strHTMLContent.Append("<tdstyle='width:100px; background:
# 99CC00'><b>Column 3</b>
 <  / td >".ToString());strHTMLContent.Append(" <  / tr > ".ToString());

// First Row Data
strHTMLContent.Append("<tr>".ToString()); strHTMLContent.Append(
  "<td style='width:100px'>a</td>" .ToString()); strHTMLContent.Append(
  "<td style='width:100px'>b</td>" .ToString()); strHTMLContent.Append(
  "<td style='width:100px'>c</td>" .ToString());strHTMLContent.Append("</tr>"
  .ToString());

// Second Row Data
strHTMLContent.Append("<tr>".ToString()); strHTMLContent.Append(
  "<td style='width:100px'>d</td>" .ToString()); strHTMLContent.Append(
  "<td style='width:100px'>e</td>" .ToString()); strHTMLContent.Append(
  "<td style='width:100px'>f</td>" .ToString()); strHTMLContent.Append("</tr>"
  .ToString());

strHTMLContent.Append("</table>".ToString());

strHTMLContent.Append("<br><br>".ToString()); strHTMLContent.Append(
  "<p align='Center'> Note : This is adynamically generated 
  word document  </p> ".ToString());
HttpContext.Current.Response.Write(strHTMLContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();

In the above listing, first the Response object is cleared,charset is set to empty and then Content type is set to MS Word.

Filename is defined for the document which will be generated through this code and this is set to the “Content-Disposition” header of the Response object.

Now to the StringBuilder variable — it is an HTML representation of the heading and table with three columns and two rows along with column headers and data appended. This content will be displayed in the
Word document when it is generated.

Once the contents to be displayed in the document are set, the Response object’s Write, End and Flush methods send the output to the browser, where the user’s copy of Microsoft Word will open the content and render it as a Word document.

Run the Web Application

Once the application is run, the following screen is displayed in the browser.

Figure 2

Click the Generate Document button in the above screen upon which the following prompt will be displayed to open or save the document.

Figure 3

Now save the document to some location in the system and then open it to see the contents in the document. Document contents will be as follows:

Figure 4

Downloads

[Download Sample]

Conclusion

In this article, we have seen how easy it is to dynamically generate Word reports in ASP.NET using HTML.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s