在vb .net中将html转换为单词时,页眉和页脚出现两次 [英] Header and footer appearing twice while converting html to word in vb .net
问题描述
大家好,
我正在使用vb.net从html创建一个word文档。我已经在类似于本网站提供的代码的帮助下成功完成了这项工作。但是当我打印该页面或预览时,页码在最后一页出现两次。这似乎是因为页眉和页脚重复。
以下是代码 -
< pre>
< pre lang = vb> Dim UserId As String =& quot;& quot;
Dim index As Integer
Dim strPath As String
Dim strLogin As String = Request.ServerVariables(& quot; LOGON_USER& quot;)
index =(strLogin.IndexOf(& quot; \& quot;))
strLogin = strLogin.Substring(index) + 1 )
strLogin = EmployeeReviewId& amp; &安培; QUOT; _&安培; QUOT; &放大器;放大器; lblEmployeeName.Text
' strLogin = strLogin& amp; &安培; QUOT; _ [&安培; QUOT; &放大器;放大器; Session.SessionID& amp; &安培; QUOT;]&安培; QUOT;
Dim guidResult As String = System.Guid.NewGuid()。ToString()
strLogin = strLogin& amp; &安培; QUOT; _ [&安培; QUOT; &放大器;放大器; guidResult& amp; &安培; QUOT;]&安培; QUOT;
Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
Dim hw As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(sw )
hw.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Html)
strFileName1 = strLogin& amp; &安培; QUOT; _&安培; QUOT; &放大器;放大器; strFileName& amp; &安培; QUOT; .DOC&安培; QUOT;
strPath = Server.MapPath(& quot; TempReport& quot;)
strPath = strPath +& quot; \& quot; &放大器;放大器; strFileName1
RenderControlsCurrent(hw)
hw.RenderEndTag()
Response.Charset =& quot;& quot;
Response.ContentType =& quot; application / ms-word& quot;
HttpContext.Current.Response.ContentType =& quot; application / msword& quot;
HttpContext.Current.Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8
Response.AddHeader(& quot; Content-Disposition& quot;,& quot; filename =& quot;& amp; strFileName& amp;& quot; .doc& quot;)
Response.Write(& quot;& lt; html& quot;& amp; _
& quot ; xmlns:o =' urn:schemas-microsoft-com:office:office ' & quot; & amp; _
& quot; xmlns:w =' urn:schemas-microsoft-com:office:word ' & quot;& amp; _
& quot; xmlns =' http: // www.w3.org/TR/REC-html40'>")
Response.Write(& quot;& lt; ; head& gt;& quot;)
Response.Write(& quot;& lt;! - [ if gte mso 9 ]& gt;& quot; &放大器;放大器; _
& quot;& lt; xml& gt;& quot; &放大器;放大器; _
& quot;& lt; w:WordDocument& gt;& quot; &放大器;放大器; _
& quot;& lt; w:View& gt; Print& lt; / w:View& gt;& quot; &放大器;放大器; _
& quot;& lt; w:Zoom& gt; 90& lt; / w:Zoom& gt;& quot; &放大器;放大器; _
& quot;& lt; w:DoNotOptimizeForBrowser /& gt;& quot; &放大器;放大器; _
& quot;& lt; / w:WordDocument& gt;& quot; &放大器;放大器; _
& quot;& lt; / xml& gt;& quot; &放大器;放大器; _
& quot;& lt;![endif] - & gt;& quot;)
Response.Write(& quot;& lt; META HTTP -EQUIV =& quot;& quot; Content-Type& quot;& quot; CONTENT =& quot;& quot; text / html; charset = UTF-8& quot;& quot;& gt ;& quot;)
Response.Write(& quot;& lt; meta name = ProgId content = Word.Document& gt;& quot;)
Response.Write(& quot ;& lt; meta name = Generator content =& quot;& quot; Microsoft Word 9& quot;& quot;& gt;& quot;)
Response.Write(& quot; & lt; meta name = Originator content =& quot;& quot; Microsoft Word 9& quot;& quot;& gt;& quot;)
Response.Write(& quot;& ; lt; style& gt; body {display:block; page:Section1; counter-Reset:Page 1 }& quot;)
Response.Write (& quot; div.Section1 {page:Section1; page- break -after:avoid;}& qu ot;)
Response.Write(& quot; div.Section2 {page:section2; page- break -after:avoid;}& quot; )
Response.Write(& quot; @page Section1 {size: 841 .7pt 595 .45pt; mso-page-orientation:landscape; margin:.5in .5in .4in .5in; mso-header-margin:.1in; mso-footer-margin:.1in; mso-paper-source: 0 ;}& quot;)
Dim head As String =& quot; @page Section1 { MSO的页脚:F1; MSO头:H1;}&安培; QUOT; &放大器;放大器; _
& quot; div.Section1 {page:Section1; page- break -after:avoid;} tr {page- break -before:always; page- < span class =code-keyword> break -inside:avoid;}& quot; &放大器;放大器; _
& quot;& lt; / style& gt;& lt; / head& gt;& quot;
Response.Write(head)
Response.Write(& quot;& lt; body& gt;& quot;)
Response.Write(& quot;& lt ; div class = Section1& gt;& quot;)
Dim oStringWriter As New System.IO.StringWriter()
Dim oHtmlTextWriter As System .Web.UI.HtmlTextWriter = New HtmlTextWriter(oStringWriter)
oHtmlTextWriter.Write(sw)
Response.Output.Write(oStringWriter.ToString())
Response.Write(& quot;& lt; span clear =& quot;& quot; all& quot;& quot; style =& quot;& quot; page- break -before:always& quot;& quot;& gt;& lt; / span& gt;& lt; div style =& quot;& quot ; mso-element:footer; font-family:GEORGIA; font-size:11pt; text-align:center& quot;& quot; id =& quot;& quot; f1& quot;& quot;& ; gt;& quot;)
Response.Write(& quot;& lt; span style = mso-tab-count:2'& gt;& lt; / span& gt;& lt; span style = ' mso-field-code:& quot;& quot; PAGE& quot;& quot;'& gt;& quot;)
Response.Write(& quot;& lt; / span& gt;& lt; / p& ; gt;& lt; / div& gt;& quot;)
Response.Write(& quot;& lt; / div& gt;& quot;)
Response.Write (& quot;& lt; / body& gt;& quot;)
Response.Write(& quot;& lt; / html& gt;& quot;)
HttpContext .Current.Response.Flush()
Response.End()
< / pre>< / pre>
这里RenderControlsCurrent(hw)是包含要转换为word的html的方法。 hw的类型为HtmlTextWriter
请帮我这个。
先谢谢。
Karthik
请不要在不添加实际信息的情况下重新发布您的问题:如果您没有回复,那么它只会烦恼人
Hi All,
I am creating a word doc from an html using vb.net. I have done that successfully with the help of the code similar to the one given in this site. But when I print that page or when I preview it, page number appears twice in the last page. This seems to be because the header and footer is repeating.
Following is the code-
<pre>
<pre lang="vb">Dim UserId As String = ""
Dim index As Integer
Dim strPath As String
Dim strLogin As String = Request.ServerVariables("LOGON_USER")
index = (strLogin.IndexOf("\"))
strLogin = strLogin.Substring(index + 1)
strLogin = EmployeeReviewId & "_" & lblEmployeeName.Text
'strLogin = strLogin & "_[" & Session.SessionID & "]"
Dim guidResult As String = System.Guid.NewGuid().ToString()
strLogin = strLogin & "_[" & guidResult & "]"
Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
Dim hw As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(sw)
hw.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Html)
strFileName1 = strLogin & "_" & strFileName & ".doc"
strPath = Server.MapPath("TempReport")
strPath = strPath + "\" & strFileName1
RenderControlsCurrent(hw)
hw.RenderEndTag()
Response.Charset = ""
Response.ContentType = "application/ms-word"
HttpContext.Current.Response.ContentType = "application/msword"
HttpContext.Current.Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8
Response.AddHeader("Content-Disposition", "filename=" & strFileName & ".doc")
Response.Write("<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'>")
Response.Write("<head>")
Response.Write("<!--[if gte mso 9]>" & _
"<xml>" & _
"<w:WordDocument>" & _
"<w:View>Print</w:View>" & _
"<w:Zoom>90</w:Zoom>" & _
"<w:DoNotOptimizeForBrowser/>" & _
"</w:WordDocument>" & _
"</xml>" & _
"<![endif]-->")
Response.Write("<META HTTP-EQUIV=""Content-Type"" CONTENT=""text/html; charset=UTF-8"">")
Response.Write("<meta name=ProgId content=Word.Document>")
Response.Write("<meta name=Generator content=""Microsoft Word 9"">")
Response.Write("<meta name=Originator content=""Microsoft Word 9"">")
Response.Write("<style> body {display: block; page: Section1; counter-Reset: Page 1}")
Response.Write("div.Section1 {page:Section1;page-break-after: avoid;}")
Response.Write("div.Section2 {page:Section2;page-break-after: avoid;}")
Response.Write("@page Section1 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:.5in .5in .4in .5in;mso-header-margin:.1in;mso-footer-margin:.1in;mso-paper-source:0;}")
Dim head As String = "@page Section1 {mso-footer:f1;mso-header:h1;}" & _
"div.Section1{page:Section1; page-break-after: avoid;}tr{page-break-before:always;page-break-inside:avoid;}" & _
"</style></head>"
Response.Write(head)
Response.Write("<body>")
Response.Write("<div class=Section1>")
Dim oStringWriter As New System.IO.StringWriter()
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(oStringWriter)
oHtmlTextWriter.Write(sw)
Response.Output.Write(oStringWriter.ToString())
Response.Write("<span clear=""all"" style=""page-break-before:always""></span><div style=""mso-element:footer;font-family: GEORGIA; font-size: 11pt; text-align:center"" id=""f1"" >")
Response.Write("<span style='mso-tab-count:2'></span><span style='mso-field-code:"" PAGE ""'>")
Response.Write("</span></p></div>")
Response.Write("</div>")
Response.Write("</body>")
Response.Write("</html>")
HttpContext.Current.Response.Flush()
Response.End()
</pre></pre>
Here RenderControlsCurrent(hw) is the method which contains the html to be converted to word. hw is of type "HtmlTextWriter"
Please help me with this.
Thanks in Advance.
Karthik
Please do not repost your question without adding actual information: if you haven't had a reply, then it will only annoy people.
这篇关于在vb .net中将html转换为单词时,页眉和页脚出现两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!