使用固定的页眉和页脚将html导出为word文档 [英] export an html to word document with fixed headers and footers
本文介绍了使用固定的页眉和页脚将html导出为word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在尝试将html导出到word文档。以下是按钮点击时使用的代码:
Hi
I am trying to export an html to word document.the following is the code that m using on button click:
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
推荐答案
这里有解决方案 http://mvark.blogspot.com/2010/09/how-to-dynamically-generate-word.html [ ^ ]
Solution is available here http://mvark.blogspot.com/2010/09/how-to-dynamically-generate-word.html[^]
这篇关于使用固定的页眉和页脚将html导出为word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文