posts - 50, comments - 92, trackbacks - 171

My Links

News

Article Categories

Archives

Post Categories

Image Galleries

Friends Blog

Webbased MS Word dynamic document generation in C# using HTML tags with hidden tables.

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...

 


 

 

 

 

Print | posted on Wednesday, July 27, 2005 2:02 PM | Filed Under [ C# ]

Feedback

Gravatar

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

You realize that HttpContext.Current is a costly call?

pack the context reference into a var
7/27/2005 8:17 PM | Eric Newton
Gravatar

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

Really this code helping me. I'm using this code to create word file instead of Crystal Report.
9/17/2005 2:28 PM | Balamurugan
Gravatar

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

Realy Helpfull... Thanks... But would like to insert page break also. Pls. guide
9/22/2005 6:14 PM | Man
Gravatar

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

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.
10/6/2005 12:20 PM | harjeet chandwani
Gravatar

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

Realy Helpfull... Thanks... But would like to insert page break And insert Images , It should be in print layout. Pls. guide
11/15/2005 11:50 AM | Vijay Kumar. A
Gravatar

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

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.
1/9/2006 4:55 PM | Karl
Gravatar

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

Brilliant article - This has helped me enormiously.
2/12/2007 1:04 AM | John Gates
Gravatar

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

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.
3/17/2007 11:20 PM | Kafa
Gravatar

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

Simple but genious! But you cannot insert page breaks, right?
8/2/2007 4:22 PM | Schleicher
Gravatar

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

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...
11/5/2007 9:34 PM | Neeraj
Gravatar

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

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.
4/27/2008 11:57 PM | Abhishek
Gravatar

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

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.
12/12/2008 8:42 PM | Anuj
Gravatar

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

Brilliant article
great
but i have a question about the performance
1/7/2009 2:14 AM | Nezo1985
Gravatar

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

Nice article..

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


thanks in advance.
2/17/2009 10:33 AM | Satish
Gravatar

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

Why am i lossing my cyrillic chars.

Hellp
4/28/2009 7:21 AM | B
Gravatar

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

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>");
9/9/2009 3:05 AM | Kunal
Gravatar

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

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.
9/30/2009 2:33 PM | Andrew
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: