Go to DotNetFunda.com
 Online : 645 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Generating Ms Word document in ASP.NET and C#

Submit Article | Articles Home | Search Articles |

Generating Ms Word document in ASP.NET and C#

2 vote(s)
Rating: 4.5 out of 5
red flag  Posted on: 9/11/2007 4:49:45 PM by Raja | Views: 18706 | Category: ASP.NET | Level: Intermediate


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.

Download


 Download source code for Generating Ms Word document in ASP.NET and C#





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.
If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Raja

Latest Articles

About Raja Dutta

Experience:5 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, June 02, 2008
Level:Starter
Status: [Member]
Biography:
 Response(s)
Posted by: Poster | Posted on: 16 Oct 2008 10:15:38 AM

Really simple.

Thanks Raja.

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

Submit Article

About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 3:51:08 AM