Skip to main content

Dynamic Generation of Word Document Report in ASP.NET with HTML


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.

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.

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

Comments

Popular posts from this blog

Editing Child GridView in Nested GridView

Editing Child GridView in Nested GridView In this article we will explore how to edit child gridview in the nested gridview.   Let''s write some code. Step 1:  Add scriptmanager in the aspx page. < asp : ScriptManager   ID ="ScriptManager1"   runat ="server"   EnablePageMethods ="true"> </ asp : ScriptManager > Step 2:  Add below stylesheet for modal popup. < style   type ="text/css">        .modalBackground        {              background-color : Gray;              filter : alpha(opacity=80);              opacity : 0.5;       }        .ModalWindow        {              border : solid1px#c0c0c0;              background : #f0f0f0;              padding : 0px10px10px10px;              position : absolute;              top : -1000px;       } </ style > Step 3:   Create an aspx page and add a Gridview with another gridview in the last TemplateField. The last templatefield will also contain a lable which will

Scrollable Gridview With fixheader using JQuery in Asp.net

Scrollable Gridview With fixheader using JQuery in Asp.net Introduction: In this article I will explain how to implement scrollable gridview with fixed header in asp.net using JQuery.  Description:  In Previous posts I explained lot of articles regarding Gridview. Now I will explain how to implement scrollable gridview with fixed header in asp.net. I have one gridview that contains lot of records and I used  paging for gridview  but the requirement is to display all the records without paging. I removed paging at that time gridview occupied lot of space because it contains more records to solve this problem we implemented scrollbar.  After scrollbar implementation if we scroll the gridview we are unable to see Gridview header.   To implement Scrollable gridview with fixed header I tried to implement concept with css and JavaScript but there is no luck because maintaining fixed header working in IE but not in Mozilla and vice versa to solve this browser compatibility proble

Nested GridView Example In Asp.Net With Expand Collapse

This example shows how to create Nested GridView In Asp.Net Using C# And VB.NET With Expand Collapse Functionality. I have used JavaScript to Create Expandable Collapsible Effect by displaying Plus Minus image buttons. Customers and Orders Table of Northwind Database are used to populate nested GridViews. Drag and place SqlDataSource from toolbox on aspx page and configure and choose it as datasource from smart tags Go to HTML source of page and add 2 TemplateField in <Columns>, one as first column and one as last column of gridview. Place another grid in last templateField column. Markup of page after adding both templatefields will like as shown below. HTML SOURCE 1: < asp:GridView ID ="gvMaster" runat ="server" 2: AllowPaging ="True" 3: AutoGenerateColumns ="False" 4: DataKeyNames ="CustomerID" 5: DataSour