Go to DotNetFunda.com
 Welcome, Guest!  
LoginLogin  
Take a break and be productive! read technical jokes.
 Skip Navigation Links Home > Articles > ASP.NET > Generating Ms Word document in ASP.NET and C#

All Articles | Submit Article |

Generating Ms Word document in ASP.NET and C#

 Download source file
 Posted on: 9/11/2007 4:49:45 PM by Raja | Views: 7761 | Category: ASP.NET | Level: Intermediate | Print Article
It is very frequntly asked question in any of the forum or question-answer section on websites. So I decided to write a very compact article with source code.

It is as simple as 123.

 Get Career Counseling  
DotNetFunda.Com brings you a FREE Career Counseling section where you can ask any type of career related question. Ask now!



Lets start with creating a simple .aspx page where we will give a text box to enter user's Name and a button to click on. After clicking on the button Ms Word document will be generated.

.aspx page

The code will be
<form id="form1" runat="server">

<div>
Write your name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="req1" runat="server" ControlToValidate="txtName" Text="*"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn" runat="server" OnClick="GenerateMsWordDoc" Text="Generate Ms Word Document" />
</div>
</form>


When button will be clicked, GenerateMsWordDoc event will fire.

Now lets write the code behind code. What we are going to do here is we are generating a dynamic html content based on the textbox value. Also we are writing one table with 2 cell and at last we are adding some text.

.cs file code


protected void GenerateMsWordDoc(object sender, EventArgs e)

{
string strBody = "<html>" +
"<body>" +
"<div>Your name is: <b>" + txtName.Text + "</b></div>" +
"<table width="100%" style="background-color:#cfcfcf;"><tr><td>1st Cell body data</td><td>2nd cell body data</td></tr></table>" +
"Ms Word document generated successfully." +
"</body>" +
"</html>";
string fileName = "MsWordSample.doc";
// You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader ("Content-disposition", "attachment; filename="+ fileName);
Response.Write(strBody);
}


Thats we have to do!!!.

Just download the sample code and start using it.

Interesting?   Bookmark and Share


About Raja Dutta

Experience:4 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, June 02, 2008
Biography:--
Thanks
Raja
 Latest post(s) from Raja

   ◘ Forms Authentication in ASP.NET with C#: Advance posted on 9/20/2008 9:58:02 AM
   ◘ Forms Authentication in ASP.NET with C#: Basic posted on 7/30/2008 4:08:34 PM
   ◘ Getting selected value from asp:RadioButtonList in JavaScript posted on 4/15/2008 4:03:49 AM
   ◘ Reading appSettings and connectionStrings from web.config file in asp.net posted on 10/9/2007 7:14:12 PM
   ◘ Generating Ms Word document in ASP.NET and C# posted on 9/11/2007 4:49:45 PM


Response(s) to this Article
Posted by: Spentsarsky | Posted on: 01 Feb 2009 08:53:06 PM
Here is the component that generates document based on the custom template. The documents are generated from the sharepoint list ... so the data is pulled from the list item into the document on the fly:
http://store.sharemuch.com/products/generate-word-documents-from-sharepoint-list

Hope that helps,

Yaroslav Pentsarskyy
Blog: www.sharemuch.com
Posted by: Poster | Posted on: 16 Oct 2008 10:15:38 AM
Really simple.

Thanks Raja.

 Submit Article

About Us | Contact Us | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)