JS-WORD完善结合之模板化

在实际开发中,会遇到在WORD提供的模板中,插入特定的信息。如下模板:

JS操作WORD测试

姓名

${name}

性别

${sex}

民族

${mz}

${photo}

住址

${address}

${name}

${name}

${name}

${name}

${name}

${name}

${name}

${name}

${name}

 生成结果:

JS操作WORD测试

姓名

涛哥

性别

民族

住址

地球3

涛哥

涛哥

涛哥

涛哥

涛哥

涛哥

涛哥

涛哥

涛哥

 代码如下:

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
  <title>JS test</title>
 </head>
 <body>
  <button id='testBtn' οnclick='onTest()'>TEST</button>
 </body>
 <script>
  function onTest()
  {
   var po = {};
   po.name = '涛哥';
   po.sex = '男';
   po.mz = '汉';
   po.address = '地球3号'; 
   po.template = 'http://localhost:9090/log/jsword/jstest.docx';
   po.photo = 'https://i-blog.csdnimg.cn/blog_migrate/e1dde8a9035ab3675c5d29d11bf7a4b9.gif';
   exportWord(po);
  }

  function exportWord(po)
  {
   var wApp = new ActiveXObject("Word.Application");
   wApp.Application.Visible = true;
   var doc = wApp.Documents.Open(po.template);
   
   var fnd = doc.Content.Find;
   
   var bOK = fnd.Execute('${name}', true, true, false, false, false, true, 0 , false, po.name, 2);
   bOK = fnd.Execute('${sex}', true, true, false, false, false, true, 0 , false, po.sex, 2);
   bOK = fnd.Execute('${mz}', true, true, false, false, false, true, 0 , false, po.mz, 2);
   bOK = fnd.Execute('${address}', true, true, false, false, false, true, 0 , false, po.address, 2);
   
   var cell = doc.Tables(1).Cell(1,7);
   cell.Range.InlineShapes.AddPicture(po.photo);
   
   //Execute('${name}', true, true, false, false, false, true, WdFindWrap , false, name, wdReplaceAll, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl, MatchPrefix, MatchSuffix, MatchPhrase, IgnoreSpace, IgnorePunct);
   
  }
 </script>
</html>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值