Create a word document from a web server.

  • Thread starter Thread starter chris
  • Start date Start date
C

chris

I'm generating a report on a web site based on user entered query
parameters.
Obviously, displaying it as a web page is dead easy.
Now, they want me to offer a "download as Word" option. (RTF would do
as well - There are no tables or other fancy things.)
I have no experience binding to Office apps. I'm pretty well a data-
structures and web controls guy.
Is this something I can learn in a week or should I outsource the
task?

So, I figure the problem steps are:
1. Open some sort of WordDocument object;
2. Add some sort of WordDomTreeNode iteratively,
3. Write to an output stream. // Or do I need to create a physical
file and offer a link?
4. Profit!

The problem is that I can't find any documentation on how to do this.

Does anyone have any experience with this sort of thing?
Is it even possible?
Any links to a good tutorial.
 
Word handles HTML very nicely, so you can use a variation on the following
(make sure there is nothing in the ASPX page except the @Page declaration:

string strDocBody;

try { strDocBody = "<html " +
"xmlns:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office:word'" +
"xmlns='http://www.w3.org/TR/REC-html40'>" + "<head>" + "<title>Dynamic
Generated Document</title>";


strDocBody = strDocBody + "<!--[if gte mso 9]>" + "<xml>" +
"<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:Zoom>100</w:Zoom>" +
"<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" +
"<![endif]-->";


strDocBody = strDocBody + "<style> @page" + "{size:8.5in 11.0in;
mso-first-footer:ff1; mso-footer: f1; mso-header: h1; border:solid navy
2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt;" + " margin:0.75in 0.50in 0.75in
0.50in ; " + " mso-header-margin:.5in; " + " mso-footer-margin:.5in;
mso-paper-source:0;}" + " div.Section1" + " {page:Section1;}" + "p.MsoFooter,
li.MsoFooter, div.MsoFooter{margin:0in; margin-bottom:.0001pt;
mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in;
font-size:12.0pt; font-family:'Arial';}" + "p.MsoHeader, li.MsoHeader,
div.MsoHeader {margin:0in; margin-bottom:.0001pt;
mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in;
font-size:12.0pt; font-family:'Arial';}" + "-->" + "</style>" + "</head>";


strDocBody = strDocBody + "<body lang=EN-US style='tab-interval:.5in'>" +
"<div class=Section1>" + "<h1>This is my Heading</h1>" + "<h2>This is my Sub
Heading</h2>" + "<p style='color:navy;'> This is blue text</p>" + "<p
style='font-weight:bold; color:green;'><u> This is green bold underlined text
</u></p>" + "<p style='color:red'><I>" + DateTime.Now + "</I></p>" + "<img
img width=217 height=162 id='myImg'
src='C:/WINDOWS/Web/Wallpaper/Autumn.jpg'>" + "<!--[if supportFields]>" +
"<div style='mso-element:header' id=h1><p class=MsoHeader><span
style='mso-tab-count:4'></span><span style='mso-field-code: PAGE '></span>
</p></div>" + "<div style='mso-element:footer' id=f1> " + "<p class=MsoFooter
style='border:none;mso-border-bottom-alt:solid windowtext
..75pt;padding:0in;mso-padding-alt:0in 0in 1.0pt 0in'><o:p> </o:p></p> " +
"Page <span style='mso-field-code: PAGE '><span
style='mso-no-proof:yes'>1</span></span> of <span style='mso-field-code:
NUMPAGES '></span>" + " <span style='mso-tab-count: 12'> <span
style='mso-field-code: DATE '></span> " + " </p></div><![endif]-->" + "</div>
</body> </html> ";

//Force this content to be downloaded as a Word document
Response.AddHeader("Content-Type", "application/msword");
Response.AddHeader("Content-disposition", "attachment; filename=mydoc.doc");
Response.Charset = ""; Response.Write(strDocBody);
}

catch (Exception ex)

{

Response.Write(ex.Message);

}

}

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
 
Back
Top