Geeks With Blogs
Information -> Source for Next Generation brcraju@hotmail.com

Here is the dynamic MS Word document generation in C#, You can use HTML tags and style sheets,
In this example table is designed not to appear even in non printable view. Usually tables will appear in word though they will not be seen when printed, In this example I have changed the border font color to white so as not to appear even in grey color, You can see MS specific style sheet tags:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer =true;
HttpContext.Current.Response.ContentType="application/msword";
HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename=InvDetails.doc");
HttpContext.Current.Response.Charset = "utf-8"; //UTF8
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250"); //windows-1250
HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Verdana;'>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#ffffff' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Verdana; background:white;border-collapse:collapse;border:none;mso-border-alt:solid white .5pt;mso-border-right-alt:dotted white .5pt; mso-padding-alt:0in 0in 0in 0in;mso-border-insideh:.5pt dotted white; mso-border-insidev:.5pt dotted white'> <TR> <TD width=300>");   
HttpContext.Current.Response.Write("MY First Row First cell Data");
HttpContext.Current.Response.Write("</Td><TD>");
HttpContext.Current.Response.Write("My First Row Second Cell Data");
HttpContext.Current.Response.Write("</Td></TR><TR><TD width=300>");
HttpContext.Current.Response.Write("MY Second Row First cell Data");
HttpContext.Current.Response.Write("</Td><TD>");
HttpContext.Current.Response.Write("My Second Row Second Cell Data");
HttpContext.Current.Response.Write("</Td></TR></TABLE>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
HttpContext.Current.Response.Write("<U>");
HttpContext.Current.Response.Write("UnderLined data");
HttpContext.Current.Response.Write("</U>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
HttpContext.Current.Response.Write("<P>");
HttpContext.Current.Response.Write("This is paragraph data which can be placed in single line, when it appears in word it will have autofit. Today's date in specific format::");1
HttpContext.Current.Response.Write(String.Format("{0:d-MMMM-yyyy}", DateTime.Now));   
HttpContext.Current.Response.Write("</P>");
HttpContext.Current.Response.Write("</font>");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

Here it ends...

 


 

 

 

 

Posted on Wednesday, July 27, 2005 2:02 PM C# | Back to top


Comments on this post: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
You realize that HttpContext.Current is a costly call?

pack the context reference into a var
Left by Eric Newton on Jul 27, 2005 8:17 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Really this code helping me. I'm using this code to create word file instead of Crystal Report.
Left by Balamurugan on Sep 17, 2005 2:28 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Realy Helpfull... Thanks... But would like to insert page break also. Pls. guide
Left by Man on Sep 22, 2005 6:14 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
i want to generate excel sheet using html tags,i am able to do so. only the problem i am facing is that how to apply excel sheet cell format in it. I want to apply the following style in the cell of excel sheet.
mso-number-format:@;
The above style converts the cell of excel in the text.
Left by harjeet chandwani on Oct 06, 2005 12:20 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Realy Helpfull... Thanks... But would like to insert page break And insert Images , It should be in print layout. Pls. guide
Left by Vijay Kumar. A on Nov 15, 2005 11:50 AM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Love it! So obvious yet nobody else is doing it! I knew there had to be a way. Thanks for pointing me in the right direction.
Left by Karl on Jan 09, 2006 4:55 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Brilliant article - This has helped me enormiously.
Left by John Gates on Feb 12, 2007 1:04 AM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Love it! So obvious yet nobody else is doing it! I knew there had to be a way. Thanks for pointing me in the right direction.
Left by Kafa on Mar 17, 2007 11:20 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Simple but genious! But you cannot insert page breaks, right?
Left by Schleicher on Aug 02, 2007 4:22 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
please let me if i am wrong...
1. this is not Word Doc...
2. this is only a simple Html...
3. since word can open html... and you are adding HttpContext.Current.Response.ContentType="application/msword";

this file will open in msword....

note: creating html and creating MS Word(doc) is different thing...
Left by Neeraj on Nov 05, 2007 9:34 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
How i can add header and fotter in doc file using above code in below link.

http://geekswithblogs.net/brcraju/archive/2005/07/27/48372.aspx

Please help me.

thanks in advance.
Left by Abhishek on Apr 27, 2008 11:57 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
In the head :

<style>
@page Section1 {mso-footer:f1;}
div.Section1{page:Section1;}
p.MsoFooter, li.MsoFooter, div.MsoFooter{
mso-pagination:widow-orphan;
tab-stops:center 216.0pt right 432.0pt;}
</style>

In the Body :

<div style="mso-element:footer" id="f1">
<p class="MsoFooter"><span style='mso-field-code:" FILENAME "'> </span>
<span style='mso-tab-count:2'></span>Page <span style='mso-field-code:" PAGE "'></span> of <span style='mso-field-code:" NUMPAGES "'></span></p>
</div>

and you will have footer.
Left by Anuj on Dec 12, 2008 8:42 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Brilliant article
great
but i have a question about the performance
Left by Nezo1985 on Jan 07, 2009 2:14 AM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Nice article..

How to save this document in this path "c:\\" without download prompt.


thanks in advance.
Left by Satish on Feb 17, 2009 10:33 AM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Why am i lossing my cyrillic chars.

Hellp
Left by B on Apr 28, 2009 7:21 AM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
To render the Row in the Grid View :

Response.write("<table>");
Response.write("<tr>");
Response.write("<td>");
Response.write("NAME");

Response.write("</td>");
Response.write("<td>");

Response.write("AJANTA");

Response.write("</td>");
Response.write("</tr>");
Response.write("<tr>");
Response.write("<td>");
Response.write("AGE");

Response.write("</td>");
Response.write("<td>");

Response.write("26");

Response.write("</td>");
Response.write("</tr>");

Response.write("</table>");
Left by Kunal on Sep 09, 2009 3:05 AM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Good lord, you must get paid by the character and line. This is terrible, ugly code. Learn to use variables, and like the poster said above, HttpContext.Current is a costly call. You should also look into the StringBuilder.
Left by Andrew on Sep 30, 2009 2:33 PM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
The solution provided to add footer/header does not work as expected, the text in header or footer is printed somewhere in the body of the document.

<div style="mso-element:footer" id="f1">
<p class="MsoFooter">
<span style='mso-tab-count:2'></span> <span style='mso-field-code:" PAGE "'></span></p>
</div>


This solution will work but is not in the format I am looking for, any suggestions? I would like show in the header as "Page X of Y"
Left by sv on Feb 10, 2010 8:37 AM

# cheap sale gucci clothing, louis vuitton handbags, coach handbags
Requesting Gravatar...

3ahandbags.com Competitive prices, honest service and rest assured at online shopping,
Our company mainly dealing in shoes,besides,we also sell urban hip hop clothes...etc...
I am looking forward to your early reply and visit our website...

Dropship to worldwide, start from One Piece!!
I think we may be able to work together in the future...

thanks
best regards

The website is: http://www.3ahandbags.com
Wholesale NFL,NBA,MLB,NHL Sport Jerseys From China at ( www.cheapnflchina.com )
Left by qingc on Dec 12, 2010 9:32 AM

# re: Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.
Requesting Gravatar...
Sale digital camera Battery charger, battery Eletronics.Here area variety of
Left by provide battery charger on Mar 24, 2011 4:02 AM

Your comment:
 (will show your gravatar)


Copyright © Raju | Powered by: GeeksWithBlogs.net