6

我正在使用 html 代码成功生成 word 文档,其中页眉和页脚样式为 css 打印模式,这是我的代码:

<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>Mon document</title>
<meta charset=\"UTF-8\" />
<!--[if gte mso 9]>
<xml><w:WordDocument><w:View>Print</w:View><w:Zoom>100</w:Zoom><w:DoNotOptimizeForBrowser/></w:WordDocument></xml>
<![endif]-->
<link rel=File-List href=\"mydocument_files/filelist.xml\">
<style><!-- 
@page
{
    size:21cm 29.7cmt;  /* A4 */
    margin:1cm 1cm 1cm 1cm; /* Margins: 2.5 cm on each side */
    mso-page-orientation: portrait;  
    mso-header: url(\"mydocument_files/headerfooter.htm\") h1;
    mso-footer: url(\"mydocument_files/headerfooter.htm\") f1;  
}
@page Section1 { }
div.Section1 { page:Section1; }
p.MsoHeader, p.MsoFooter { border: none; }
--></style>
</head>
<body>
<div class=Section1>
 my content
</div>
</body>
</html>

我想做的是仅在第一页上显示页眉和页脚。为此,我尝试以visibility:hidden这种方式将页眉和页脚应用于不同于第一个页面的页眉和页脚:

p.MsoHeader, p.MsoFooter { border: none; visibility: hidden;}
p.MsoHeader :first, p.MsoFooter :first { border: none; visibility: visible;}

但是页眉和页脚仍然显示在所有页面上......知道如何做到这一点吗?

4

2 回答 2

2

尝试这个:

p.MsoHeader, p.MsoFooter { border: none; display: none;}
p.MsoHeader :first, p.MsoFooter :first { border: none; display: block;}

或者,如果这不起作用,最好不要在第一次播放时调用页眉和页脚,因此只需从您不希望页眉和页脚出现的页面中删除这两行:

mso-header: url(\"mydocument_files/headerfooter.htm\") h1;
mso-footer: url(\"mydocument_files/headerfooter.htm\") f1; 
于 2016-02-16T13:44:57.997 回答
1

在比较 word 生成的 html 时,我错过了一个关键的 mso css 标签:

mso-first-header: url ...

而不是mso-header.

除此之外,该属性mso-title-page还必须设置为yes

将这两者结合起来,就可以得到想要的效果!

于 2016-02-12T12:43:39.203 回答