现在的位置: 首页 > 综合 > 正文

通过js操作word的页眉页脚

2014年03月09日 ⁄ 综合 ⁄ 共 1724字 ⁄ 字号 评论关闭

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id="table">
<table >
 <tr>
  <td>
   abc
  </td>
  <td>
   abc
  </td>
  <td>
   abc
  </td>
  <td>
   abc
  </td>
 </tr>
</table>
</div>
</body>
</html>
<script>

var wordApp=new ActiveXObject("Word.Application");

wordApp.Visible=false;     // 设置为true,则会以word程序打开;设置为false,则在后台执行,不显示word界面。
//打开指定的WORD文档
var wordDoc=wordApp.Documents.Open("d:/a.doc");

var str = new Array();
str[0]= new Array("周剑","地方","马克思","地俄方");
str[1]= new Array("毛肚","发的","马速度","老婆");

try{
/**
 * 修改页脚并保存word
 */
function exprotWord(){
 // 为页脚添加一个表格
 var myTable=wordDoc.Sections(1).Footers(1).Range.Tables.Add(wordDoc.Sections(1).Footers(1).Range,2,4);
 //  打印字段行
 for (i= 0;i<2;i++)  
 {  
  for ( n = 0; n < 4; n++)
  {
   with (myTable.Cell(i+1,n+1).Range)  
   {  
    Style.Font.Size = 15;        // 设置字号
    Style.Font.SizeBi = 15;        //
    Style.Font.Color=657930;       //
    Style.Font.Bold = 1;        // 设置粗体
    Style.Font.BoldBi = 1;
    Style.Font.NameFarEast = "方正静蕾简体";    // 设置字体
    InsertAfter(str[i][n]);        // 插入文字到表格
    Borders(1).LineStyle = 1;       // 上边线
    Borders(2).LineStyle = 1;       // 下边线
    Borders(3).LineStyle = 1;       // 左边线
    Borders(4).LineStyle = 1;       // 右边线
    ParagraphFormat.Alignment =1;        // 设置对齐方式 水平对齐   
   } 
  }
 } 
 wordDoc.SaveAs("d:/b.doc");          // 保存
}

/**
 * 插入页眉页脚
 */
function insertHeaderFoot()
{
 wordDoc.Sections(1).Headers(1).Range.Text = "页眉";    // 设置页眉内容
 wordDoc.Sections(1).Footers(1).Range.Text = "Footer text";  // 设置页脚内容
}
}catch(e){
 wordApp.quit();             // 如果出异常则退出word进程
 alert(err.description);           // 弹出错误信息
}
// 调用插入
insertHeaderFoot();
// 修改页脚并保存
exprotWord();
// 最后退出word进程
wordApp.quit();

  //还有更多属性方法,自己参考IE"开发人员工具"或 firefox的debug工具查看。
</script>

 

示例图:

抱歉!评论已关闭.