在vb .net中将html转换为单词时,页眉和页脚出现两次 [英] Header and footer appearing twice while converting html to word in vb .net

查看:66
本文介绍了在vb .net中将html转换为单词时,页眉和页脚出现两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用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'&gt;&quot;)
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 = &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; &amp; lblEmployeeName.Text
        'strLogin = strLogin &amp; &quot;_[&quot; &amp; Session.SessionID &amp; &quot;]&quot;
        Dim guidResult As String = System.Guid.NewGuid().ToString()
        strLogin = strLogin &amp; &quot;_[&quot; &amp; 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; &amp; strFileName &amp; &quot;.doc&quot;
        strPath = Server.MapPath(&quot;TempReport&quot;)
        strPath = strPath + &quot;\&quot; &amp; 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'&gt;&quot;)
        Response.Write(&quot;&lt;head&gt;&quot;)

        Response.Write(&quot;&lt;!--[if gte mso 9]&gt;&quot; &amp; _
                               &quot;&lt;xml&gt;&quot; &amp; _
                               &quot;&lt;w:WordDocument&gt;&quot; &amp; _
                               &quot;&lt;w:View&gt;Print&lt;/w:View&gt;&quot; &amp; _
                               &quot;&lt;w:Zoom&gt;90&lt;/w:Zoom&gt;&quot; &amp; _
                               &quot;&lt;w:DoNotOptimizeForBrowser/&gt;&quot; &amp; _
                               &quot;&lt;/w:WordDocument&gt;&quot; &amp; _
                               &quot;&lt;/xml&gt;&quot; &amp; _
                               &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;}&quot;)
        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-footer:f1;mso-header:h1;}&quot; &amp; _
                     &quot;div.Section1{page:Section1; page-break-after: avoid;}tr{page-break-before:always;page-break-inside:avoid;}&quot; &amp; _
         &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>




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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆