Go to DotNetFunda.com
  Welcome, Guest!  
LoginLogin  
{ Submit resources and get monthly gifts !!! }
Submit: Article | Interview Question | Joke | Question | Link || Search  
 Skip Navigation Links Home > Articles > Generating Ms Word document in ASP.NET and C#

All Articles | Post Articles

Generating Ms Word document in ASP.NET and C#

 Download source file
 Posted on: 9/11/2007 4:49:45 PM by Raja | Views: 875 | Category: ASP.NET 2.0 | 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.

 


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

- Hide Code
<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



- Hide 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.
Bookmark and Share

About Raja Dutta

Experience:3 year(s)
Home page:
Member since:Monday, June 02, 2008
Biography:--
Raja
 Latest post(s) from Raja

   ◘ 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
   ◘ Dynamically writing Meta tags on Master page from a User Control or any other class file posted on 6/21/2007 2:29:26 PM
   ◘ How to implement Remoting in ASP.NET posted on 6/21/2007 1:09:49 PM



Question: Why to use www.dotnetfunda.com google search?
Answer: This search has been especially optimized to search technical articles. You may find to-the-point results in comparison with other search.
Google
About Us | Contact Us | 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.