Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
I am trying to export an html to word document.the following is the code that m using on button click:


C#
string strDocbody = "";
       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></title>";

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

       strDocbody+="<style>" +
                                     "<!-- /* Style Definitions */" +
                                     "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;}" +
                                     "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;}";


       strDocbody+="@page Section1" +
                                       "   {size:8.5in 11.0in; " +
                                       "   margin:1.5in 0.6in 2in 0.6in ; " +
                                       "   mso-header: h1;" +
                                       "   mso-header-margin:.5in; " +
                                       "   mso-footer: f1;" +
                                       "   mso-footer-margin:.5in; mso-paper-source:0;}" +
                                       " div.Section1" +
                                       "   {page:Section1;" +
       "font-size:10.0pt;font-family:Times New Roman;mso-fareast-font-family:Times New Roman" +
                                        " }" +
                                       "-->" +
                                      "</style></head>";

       strDocbody+="<body lang=EN-US style='tab-interval:.5in'>" +
                                     "<div class=Section1>";

       //body
       System.IO.StringWriter sw1 = new System.IO.StringWriter();
       System.Web.UI.HtmlTextWriter oHtmlTextWriter1 = new System.Web.UI.HtmlTextWriter(sw1);
       divbody.RenderControl(oHtmlTextWriter1);
       strDocbody = strDocbody + oHtmlTextWriter1.InnerWriter.ToString() + "</div>";

       //header
       System.IO.StringWriter sw = new System.IO.StringWriter();
       System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(sw);
       divhead.RenderControl(oHtmlTextWriter);
       strDocbody += "<div style='mso-element:header' id=h1>" +
                                  "<p class=MsoHeader>" + oHtmlTextWriter.InnerWriter.ToString() + "</p></div>";

       //strDocbody += oHtmlTextWriter.InnerWriter.ToString();
       //
       //footer
       System.IO.StringWriter sw2 = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter2 = new System.Web.UI.HtmlTextWriter(sw2);
       divfooter.RenderControl(oHtmlTextWriter2);
       strDocbody += "<div style='mso-element:footer' id=f1>" +
                                       " <p class=MsoFooter>" + oHtmlTextWriter2.InnerWriter.ToString() + "</p></div>";

       strDocbody+="</body></html>";


          Response.AddHeader("Content-Type", "application/msword");
          //filename specify the name of word document of your choice
          Response.AddHeader("Content-disposition", "attachment; filename=myWordDoc.doc");
          //Response.Charset = "";
          Response.Write(strDocbody);

The problem i am facing is the header and footers are fixed but it repeates the header and footer on the last pae.Please guide

Thanks in advance
Posted
Updated 15-May-11 23:59pm
v2
Comments
Debojyoti Majumder 16-May-11 5:34am    
Please format your code properly..
Samarjeet Singh Tomar 16-May-11 11:06am    
Headers and footers are suppose to be repeated i think?

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900