办公室HTML Word页眉

6
我将生成一个带有HTML代码的“Word文档”第一页页眉,其中<style>标签包含以下内容:
@page Section {
    size:8.5in 11.0in;
    margin: 0.7in 0.9in 0.7in 0.9in;
    mso-header-margin: 0.0in;
    mso-footer-margin: 0.0in;
    mso-title-page: yes;
    mso-first-header: fh1;
    mso-paper-source: 0;
}
div.Section {
    page: Section;
}

<body>标签:

<div class="Section" style="font-family: Verdana, sans-serif;">
    <![if supportFields]>
        <div style="mso-element:header" id="fh1">
            <p class="MsoHeader">
                header
            </p>
        </div>
    <![endif]>
</div>

它将“header”文本同时显示在页眉和文档正文中。我找到的资源(http://techsynapse.blogspot.com/2007/03/generating-word-document-dynamically.html 和 http://www.pbdr.com/ostips/wordfoot.htm)都说,除非使用不适用于我的附加标头文件,否则无法避免此效果。
那么,我的问题来了:你知道任何解决该问题的方法吗?如何隐藏主文档中的页眉出现,但将其保留在页面顶部的位置?感谢您提前提供的任何建议。
3个回答

9
这是我的解决方案:

这对我有用:

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

<!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:View>Print</w:View>
  <w:Zoom>90</w:Zoom>
</w:WordDocument>
</xml><![endif]-->


<style>
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;
}
<style>

<!-- /* Style Definitions */

@page Section1
{
    size:8.5in 11.0in;
    margin:1.0in 1.0in 1.0in 1.0in;
    mso-header-margin:.5in;
    mso-footer-margin:.5in;
    mso-title-page:yes;
    mso-header: h1;
    mso-footer: f1;
    mso-first-header: fh1;
    mso-first-footer: ff1;
    mso-paper-source:0;
}


div.Section1
{
    page:Section1;
}

table#hrdftrtbl
{
    margin:0in 0in 0in 900in;
    width:1px;
    height:1px;
    overflow:hidden;
}
-->
</style></head>

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

<p> CONTENT </p>

<br/>
    <table id='hrdftrtbl' border='0' cellspacing='0' cellpadding='0'>
    <tr><td>

    <div style='mso-element:header' id=h1 >
        <p class=MsoHeader ><p>&nbsp;HEADER-TITLE</p></p>
    </div>

    </td>
    <td>
    <div style='mso-element:footer' id=f1>

        <p>&nbsp;FOOTER-TITLE</p>
        <p class=MsoFooter>
        <span style=mso-tab-count:2'></span>
            Page <span style='mso-field-code: PAGE '><span style='mso-no-proof:yes'></span></span> of <span style='mso-field-code: NUMPAGES '></span>
        </p>

    </div>



    <div style='mso-element:header' id=fh1>

    <p class=MsoHeader><span lang=EN-US style='mso-ansi-language:EN-US'>&nbsp;FIRST-HEADER-TITLE<o:p></o:p></span></p>

    </div>

    <div style='mso-element:footer' id=ff1>

    <p class=MsoFooter><span lang=EN-US style='mso-ansi-language:EN-US'>&nbsp;FIRST-FOOTER-TITLE<o:p></o:p></span></p>

    </div>

    </td></tr>
    </table>


</body></html>

对我来说,它总是显示了一个额外的空格。 - Ryan
1
无法从服务器生成页眉中的图像,但在本地主机上工作正常。 在服务器上生成的图像路径也没有问题。 请告诉我我错过了什么,为什么在本地主机上将图像添加到Word文件中,但在服务器上却没有添加。注意:在本地计算机上安装了MS Office,但在服务器上没有。 - Jitender Kumar
如何删除页眉部分的空白?当我在 Word 中查看时一切正常,但打印时会添加空白...有什么想法吗? - user123_456
为了去除额外的空格,您可以从页眉和页脚标签中删除<o:p></o:p>。 - eliteproxy

7

针对上面由vencedor提出的答案并去除头部/底部图像或文本中添加的额外空字符编码,您可以尝试以下操作:

<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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>

<!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:View>Print</w:View>
  <w:Zoom>100</w:Zoom>
</w:WordDocument>
</xml><![endif]-->
<style>
<!-- /* Style Definitions */
p.MsoHeader, li.MsoHeader, div.MsoHeader{
    margin:0in;
    margin-top:.0001pt;
    mso-pagination:widow-orphan;
    tab-stops:center 3.0in right 6.0in;
}
p.MsoFooter, li.MsoFooter, div.MsoFooter{
    margin:0in 0in 1in 0in;
    margin-bottom:.0001pt;
    mso-pagination:widow-orphan;
    tab-stops:center 3.0in right 6.0in;
}
.footer {
    font-size: 9pt;
}
@page Section1{
    size:8.5in 11.0in;
    margin:0.5in 0.5in 0.5in 0.5in;
    mso-header-margin:0.5in;
    mso-header:h1;
    mso-footer:f1;
    mso-footer-margin:0.5in;
    mso-paper-source:0;
}
div.Section1{
    page:Section1;
}
table#hrdftrtbl{
    margin:0in 0in 0in 9in;
} 
-->
</style>
<style type="text/css" media="screen,print">
body {
    font-family: "Calibri", "Verdana","HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
    font-size:12pt;
}
pageBreak {
  clear:all;
  page-break-before:always;
  mso-special-character:line-break;
}
</style>
</head>
<body style='tab-interval:.5in'>
<div class="Section1">

Something page 1
<div class="pageBreak"></div>
Something page 2
<div class="pageBreak"></div>

<table id='hrdftrtbl' border='1' cellspacing='0' cellpadding='0'>
        <tr>
            <td>
                <div style='mso-element:header' id="h1" >
                    <p class="MsoHeader">
                        <table border="0" width="100%">
                            <tr>
                                <td>

                                    YOUR_HEADER IMAGE/TEXT

                                </td>
                            </tr>
                        </table>
                    </p>
               </div>
          </td>
          <td>
            <div style='mso-element:footer' id="f1">
                <p class="MsoFooter">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td align="center" class="footer">
                            YOUR_FOOTER_TEXT
                                <g:message code="offer.letter.page.label"/> <span style='mso-field-code: PAGE '></span> of <span style='mso-field-code: NUMPAGES '></span>
                            </td>
                        </tr>
                    </table>
                </p>
            </div>
        </td>
    </tr>
    </table>
  </div>
</body>
</html>

现在包括完整的头部和底部段落的CSS组件。

真正去除额外编码的技巧是在MsoFooter和MsoHeader部分声明另一个表格。


非常有用,谢谢! 您知道是否有任何属性可以使正文文字重叠在页眉部分上吗? 现在,正文文字总是在页眉的最后一行下方。 - Vincent Teyssier
3
请看这里:http://sebsauvage.net/wiki/doku.php?id=word_document_generation。我们最终采用了MHTML模型(在页面底部),并且它完美地工作了。目前这种方法存在的问题是隐含的表格,我们的情况下会在最后一页超过一半时创建一个额外的页面。 - V H
是的,那正是我成功使用过的教程。但我无法让文本重叠在页眉上方。另一个选项是使用水印代替页眉,但这意味着将页眉HTML转换为图片,这也不容易 :/ 因此,如果您有什么技巧可以使正文文本从页面顶部开始,即使有页眉,那就太好了 :) - Vincent Teyssier
有什么解决方案可以移除页面末尾的隐藏表格或删除额外的空白页? - ToskaMan
根据上述http://sebsauvage.net/wiki/lib/exe/fetch.php?media=wordgen:mime_example.zip的评论 - 唯一的解决方法是完全改变MHTML。 - V H
有人能回答这个问题吗?https://stackoverflow.com/questions/56800574/html-to-ms-word-conversion-with-header-and-footer-using-javascript 我按照这个示例进行操作,但页脚没有被添加,而是只在最后一页显示了“page of”。 - Ranjith Kumar

0

您可以隐藏 此答案 后面的额外空间

限制表格的高度:

<table id='hrdftrtbl' border='1' cellspacing='0' cellpadding='0' style='margin-left:50in;'>
    <tr style='height:1pt;mso-height-rule:exactly'>
            <td>
                <div style='mso-element:header' id="h1" dir="ltr" >
                    <p class="MsoHeader">
                        <table border="0" width="100%" style="margin-bottom: 15pt;">
                            <tr>
                                <td width="50%">
                                Some text goes here
                                </td>
                                <td width="50%" style="text-align: left">
                                    <img height="auto" src="">
                                </td>
                            </tr>
                        </table>
                    </p>
               </div>
          </td>
          <td>
            <div style='mso-element:footer' id="f1">
                <p class="MsoFooter">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td align="center" class="footer" dir="ltr">
                                <g:message code="offer.letter.page.label"/> <span style='mso-field-code: PAGE '></span> / <span style='mso-field-code: NUMPAGES '></span>
                            </td>
                        </tr>
                    </table>
                </p>
            </div>
        </td>
    </tr>
    </table>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接