Click here to Skip to main content
Click here to Skip to main content

Dynamically generate a MS Word document using HTML & CSS

By , 23 Jun 2006
 

Introduction

This article explains how easy it is to generate reports dynamically in a visually rich and appealing format like MS-Word (2000 and above) without using any components, and shows a little workaround for a quirk. It also talks about the connection between Office, XML, and HTML.

Documents can be converted from Word to HTML (File->Save As) and vice versa! To create a dynamic Word report, you will need to generate regular HTML text and apply the required formatting through CSS. You can even incorporate stuff from the database into the Word report. By playing around with MIME settings, you can force the HTML content to be downloaded as a Word .doc file.

Here comes the code

When you save the downloaded Word document and open it, it opens in the Web Layout format. Now, wouldn't it be neater if it opened in the default Print Layout? Well, all you need to do is attach the style properties. Here comes the code:

Public Sub Page_Load(sender as Object, e as EventArgs)

    'build the content for the dynamic Word document
    'in HTML alongwith some Office specific style properties. 
    Dim strBody As New System.Text.StringBuilder("")

    strBody.Append("<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>Time</title>") 

    'The setting specifies document's view after it is downloaded as Print
    'instead of the default Web Layout
    strBody.Append("<!--[if gte mso 9]>" & _
                             "<xml>" & _ 
                             "<w:WordDocument>" & _
                             "<w:View>Print</w:View>" & _
                             "<w:Zoom>90</w:Zoom>" & _ 
                             "<w:DoNotOptimizeForBrowser/>" & _
                             "</w:WordDocument>" & _
                             "</xml>" & _ 
                             "<![endif]-->")

    strBody.Append("<style>" & _
                            "<!-- /* Style Definitions */" & _
                            "@page Section1" & _
                            "   {size:8.5in 11.0in; " & _
                            "   margin:1.0in 1.25in 1.0in 1.25in ; " & _
                            "   mso-header-margin:.5in; " & _
                            "   mso-footer-margin:.5in; mso-paper-source:0;}" & _
                            " div.Section1" & _
                            "   {page:Section1;}" & _
                            "-->" & _
                           "</style></head>") 

    strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" & _
                            "<div class=Section1>" & _
                            "<h1>Time and tide wait for none</h1>" & _ 
                            "<p style='color:red'><I>" & _
                            DateTime.Now & "</I></p>" & _
                            "</div></body></html>") 

    'Force this content to be downloaded 
    'as a Word document with the name of your choice
    Response.AppendHeader("Content-Type", "application/msword")
    Response.AppendHeader ("Content-disposition", _
                           "attachment; filename=myword.doc")
    Response.Write(strBody)
End Sub

Click here to try it out.

If you wish to dig deeper into other style properties that you would like to implement, create the Word document in the required fashion, save the Word document as an HTML file, and view the source for the style settings. Wonderful, right?

This article just touches the tip of the iceberg. There are other good resources too. Microsoft Office 2000 supports Hypertext Markup Language (HTML) as a native file format. To get a low-down on this technique, you can download the Microsoft Office HTML and XML Reference that describes the files saved by Excel, PowerPoint, and Word when a document, presentation, workbook, or worksheet is saved as a Web page. You can play around with the style settings to create different configurable options for the user to view a downloadable Word document.

MS Word has better support now for HTML. You can even use it as an HTML editor. The Office HTML Filter is a handy tool you can use to remove Office-specific markup tags embedded in Office 2000 documents saved as HTML to give you really lean and mean HTML code. Even the most basic Windows users are well conversant with Word. Generating HTML files could not have got easier than this, and folks new to HTML do not have to get entangled in tags.

While on the topic, developers can checkout Peter Bromberg’s article: Build a C# Multipart MIME Encoding Library to Save Web Pages in "MHT" Format, on embedding images into a Word document.

Conclusion

This article tip shows a lightweight method to generate a Word document without using any components, and show it in Print Layout. If you have any nifty Office tips related to HTML and XML, please do share.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

'Anil' Radhakrishna
Web Developer
India India
Member
'Anil' Radhakrishna is a seasoned developer who enjoys working with Microsoft tools & technologies. He blogs quite regularly about his little discoveries and technical experiments on his blog called Tech Tips, Tricks & Trivia. You can find some of his unusual code samples & snippets at his Code Gallery. He loves building mash-ups using public Web APIs.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to make its default save type is .docmemberjeevanjoshi26 Apr '13 - 1:57 
Its work fine export HTML Stream to MSWord...
 
but when I download that file if it opened directly or saved on HDD. and now I want to save it with different name.
 
when I click on save as in MSWORD , in save dialog box default save format comes .htm ,.HTML .
It should come .doc ...
 
How to do this ?
 

I tried with content type but not worked .
 

Regards ,
Jeevan Joshi.
AnswerRe: How to make its default save type is .docmemberjeevanjoshi28 Apr '13 - 22:38 
Hi ,
 
It works perfect.
 
but problem is when you open this exported .doc file and want to save with diff name , when I click on "Save as" Link in MSWORD dialog box appears in that default "Save type" comes "Web Page htm,html" it should come ".doc" .... How to do this ...
 
--
Jeevan Joshi.
GeneralMy vote of 5memberTejas Vaishnav31 Oct '12 - 20:39 
Nice
QuestionFor Header and footer with imagememberprateekfgiet17 Oct '12 - 4:50 
Hi,
 
i am able to do create doc file using your code but i want to put image in header as well as in footer in doc file how can i do it ??
 
thanks in advance
GeneralMy vote of 5memberhadi552615 Jul '12 - 19:15 
very useful and simple
GeneralMy vote of 5membergiorgio19514 Apr '12 - 3:43 
Very useful. I found it exactly what I wanted.
Thank you, Anil!
QuestionFor DOCXmemberJohnny Glenn13 Mar '12 - 2:21 
There is an easy to use alternative for DOCX format - C# / VB.NET Word component.
 
Here is a sample code how to VB.NET create Word and ASP.NET export to Word:
' Create a new empty document.
Dim document = New DocumentModel()
 
' Add document content.
document.Sections.Add(New Section(document, New Paragraph(document, "Hello World!")))
 
' Microsoft Packaging API cannot write directly to Response.OutputStream.
' Therefore we use temporary MemoryStream.
Using documentStream = New MemoryStream()
	document.Save(documentStream, SaveOptions.DocxDefault)
 
	' Stream file to browser.
	Response.Clear()
	Response.ContentType = "application/vnd.openxmlformats"
	Response.AddHeader("Content-Disposition", "attachment; filename=Document.docx")
 
	documentStream.WriteTo(Response.OutputStream)
 
	Response.End()
End Using

QuestionDocentric - Another COM free solutionmemberJure.L28 Dec '11 - 23:10 
Hi folks,
 
HTML aproach is a much better one compared to the COM interop. I just want to direct those looking for something more powerful, there is a solution for document generation that you may find interesting:
 
http://www.docentric.com
 
It is totally template-based where template designing experience is very similar to clasic reporting toolkits (like Crystal Reports, DevExpress reports), except the designer here is MS Word itself with the splecial add-in.
 
-- Jure
QuestionMarginsmemberMember 850067719 Dec '11 - 2:06 
I'm running Office 2007 and the margins I specify in the style statement don't have any effect on the actual document. I'm building forms that require margins that are smaller than MS Office's default size.
 
Any help would be appreciated.
 
Here's a code sample in C#:
 
<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>Test 1</title>
        <!--[if gte mso 9]>
            <xml>
                <w:WordDocument>
                <w:View>Print</w:View>
                <w:Zoom>90</w:Zoom>
                <w:DoNotOptimizeForBrowser/>
                </w:WordDocument>
                </xml>
        <![endif]-->
 
        <style type="text/css">
                @page Section1
                    {size:8.5in 11.0in;
                    margin:0.75in 0.625in 0.5in 0.625in;
                    mso-header-margin:.5in;
                    mso-footer-margin:.5in; mso-paper-source:0;}
                div.Section1
                        {page:Section1;}
            -->
        </style>
    </head>
    <body lang=EN-US style='tab-interval:.5in'>
        <center>
        <table align="center" width="100%" border="1" cellspacing="0" cellpadding="2">
            <tr>
                <td colspan="3" align="center" style=" font-family:arial; font-size:10pt; font-weight:bold;"><br />DEMAND ON CARRIER/CONTRACTOR<br /><br /></td>
            </tr>
            <tr>
                <td style="font-family:Arial; font-size:8.5pt" colspan="3"> 1. TO CARRIER/CONTRACTOR</td>
            </tr>
            <tr>
                <td width="50%" rowspan="2" valign="top" style="font-family:Arial; font-size:7.5pt">a. COMPANY NAME AND COMPLETE ADDRESS <i>(include Zip Code)</i></td>
                <td width="25%">test25</td>
                <td width="25%">test25</td>
            </tr>
            <tr>
                <td width="25%">test20</td>
                <td width="25%">test30</td>
            </tr>
        </table>
        </center>
    </body>
</html>

QuestionAdapt ASP code to PHPmemberPeter Trumbull9 Dec '11 - 2:58 
Hi,
 
is it possible adapt ASP code to PHP?
 
Best Regards,
 
Peter

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 23 Jun 2006
Article Copyright 2004 by 'Anil' Radhakrishna
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid