Generating Ms Word document in ASP.NET and C#

Raja
Posted by in ASP.NET category on for Intermediate level | Views : 128973 red flag
Rating: 4.33 out of 5  
 3 vote(s)

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 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.
Page copy protected against web site content infringement by Copyscape

About the Author

Raja
Full Name: Raja Dutta
Member Level:
Member Status: Member
Member Since: 6/2/2008 12:47:48 AM
Country: United States
Regards, Raja, USA
http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Poster on: 10/16/2008
Really simple.

Thanks Raja.
Posted by: Spentsarsky on: 2/1/2009
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: Seenuvasan on: 6/10/2011 | Points: 25
hi..
how to give name for this word document. because i think ,it's shows class name as filename .
Posted by: Teshow on: 9/26/2011 | Points: 25
Useful. Also, I want to show my C# code for generating Word document.

Document doc = new Document();

//Create a new secition
Section section = doc.AddSection();

//Create a new paragraph
Paragraph paragraph = section.AddParagraph();

//Append Text
paragraph.AppendText("E-Iceblue,hello");


I use a component Spire.Doc(Download here:http://www.e-iceblue.com/Download/download-word-for-net-now.html), which helps me generate Word more easily.
Posted by: Mickwen0k on: 10/19/2011 | Points: 25
Spire.Doc work quite well, I use it from last year. that's great!
Posted by: Kukurice on: 12/29/2011 | Points: 25
Hi!

Generating Microsoft Word documents using HTML is a far better approach than the Office COM (MS Office automation) one. This solution is robust but is probably limited in terms of charts, pictures, and all other MS Word concepts that are at least hard to achieve. This was a MS Word document generation solution for some quite time in our company, then we purchase Docentric toolkit (http://www.docentric.com) which pushed our previous solution into oblivion. It really helped at building templates and also ther maintinance is much better (customers frequently want to change copany logo in header, add a data field, ...). Before choosing the product we tested couple of other commercial products and docentric proved far better, simply for the following reasons: document templates and a very powerfull Word add-in.
Posted by: Zaiba on: 10/8/2013 | Points: 25
Here is another .NET WORD Component for generating word files and also for reading, modifying and even converting word files to other formats. You can also get codes from their documentation page also for adding MS Word features in your .NET Applications:


http://www.aspose.com/.net/word-component.aspx
Posted by: Siddydv27 on: 11/15/2017 | Points: 25
It is so great and fast play http://freewsopchips.net so powerful for things.

Login to post response

Comment using Facebook(Author doesn't get notification)