用JS脚本批量导出WORD的方法(可以有图片)

 该方法适用以下情况:一个列表,每一行有连接,点击可以进入另外的页面,列表旁可以放选择按钮,如果选择了,就导出该行连接页面。

该方法注意事项:1,导出的页面,CSS格式要让WORD可以识别。最好现在WORD上把页面画出,然后另存为HTML文件,注意保存其中生成的文件。

2,浏览器要准许ActiveX运行。

代码如下:

1,以下代码即可以单独导出一个页面的WORD文档。

<script id="scriptHide" language="javascript">
window.οnlοad=function()
{
    var oButton=window.document.getElementById("Button1");
    if(oButton!=null && parent.iNum>0)
    {
           oButton.οnclick=alertMessage;
           parent.wm(oButton);
    }
     else
    {
           oButton.οnclick=alertMessage;
    }
}
var odocName=null;
var omessage=null;
function alertMessage(obj)
{
     word_onclick();
     if(arguments.length==0)
     {
          if(odocName==null||odocName=="")
          {
            alert("导出失败!/r/n"+ omessage);
          }
          else
          {
            alert("导出成功!/r/n文件位置:C://" + odocName);
          }
     }
}

function   getUrlParam(name){  
          var   reg   =   new   RegExp("(^|&)"+   name   +"=([^&]*)(&|$)");  
          var   r   =   window.location.search.substr(1).match(reg);  
          if   (r!=null)   return   unescape(r[2]);   return   null;  
      }  

function word_onclick(obj)
{
      var oWD = new ActiveXObject("Word.Application");
      oWD.WindowState = 2 
      var oDC = oWD.Documents.Add("",0,1);
      var oRange =oDC.Range(0,1);
      var hd = document.getElementById("__VIEWSTATE");
      var hideTable = document.getElementById("hideTable");
      var hideTable1 = document.getElementById("hideTable1");
      var scriptHide = document.getElementById("scriptHide");
      if(hd != null)
      {
        hd.parentNode.removeChild(hd);
      }
      if(hideTable != null)
      {
        hideTable.parentNode.removeChild(hideTable);
      }
      if(hideTable1 != null)
      {
        hideTable1.parentNode.removeChild(hideTable1);
      }
      if(scriptHide != null)
      {
        scriptHide.parentNode.removeChild(scriptHide);
      }
      var sel = document.body.createTextRange();
      var lblName = document.getElementById("name");
      var docName = "Q12 个人报告.doc";
      var depno = '<%=Request.QueryString["DeptNo"].ToString()%>';
      docName = depno +"-"+ docName;
      if(lblName != null)
      {
       docName = lblName.innerText.replace(//s*$/g,"")+"-"+ docName;
      }
      
       try
       { 
          sel.execCommand("Copy");
          oRange.Paste();
          oDC.SaveAs("C://" + docName);
          odocName=docName;
        }
        catch(e)
        { 
             omessage=e.message;
        }  
        finally
        {
            oDC.Close(0);
            oWD.Quit();
            oWD = null;
        }
       if(oWD != null)
       {
           oDC.Close(0);
           oWD.Quit();
           oWD = null;
       }
    
}
  </script>

2,以下代码是用来批量导出的

<script>
 window.document.οnkeydοwn=function()
{
  if(window.event.keyCode==13)
   {
    var oLinkButton=document.getElementById('<%=this.LinkButton1.ClientID %>');
    oLinkButton.click();
   }
}
var iNum=0;
var arr=new Array;
    function CheckAll(check, id)
{
 var e = document.forms[0].elements;
 var l = e.length;
 var o;
 for(var i = 0; i < l; i++)
 {
  o = e[i];
  if (o.type == "checkbox" && o.id.indexOf(id) > -1)
  {
   if(o.disabled!=true)
   {
       o.checked = check;
   }
  }
 }
}
function BatchExport()
{
   var oDiv=window.document.getElementById("NoneDiv");
   oDiv.style.display="block";
   arr.splice(0,arr.length);
   iNum=0;
   var oCount=0;
   var oTable=window.document.getElementById('<%=this.CustDataGrid1.ClientID %>');
   if(oTable!=null)
   {
      var oCheckbox=oTable.getElementsByTagName("INPUT");
      for(var i=0;i<oCheckbox.length;i++)
      {
         if(oCheckbox[i].getAttribute("type")=="checkbox"&&oCheckbox[i].getAttribute("value")!="on"&&oCheckbox[i].checked==true)
         {
          oCount=oCount+1;
          arr[arr.length]=oCheckbox[i].getAttribute("value");
         }
         else
         {
         }
      }
      if(oCount==0)
      {
        oDiv.style.display="none";
        alert("没有选择任何项");
        return false;
      }
      iFrameUrl(arr[iNum]);
   }
   return false;
}
function iFrameUrl(STF_StaffNo)
      {
        var ohref=STF_StaffNo.split("|");
        iNum=iNum+1;
        frames["hiddenFrame"].location.href="reportQ12.aspx?LeaderNo="+ohref[0]+"&DeptNo="+ohref[1];
      }
function wm(bjct)
       {
          bjct.onclick("isofstone");
          if(iNum==arr.length)
          {
            var oDiv=window.document.getElementById("NoneDiv");
            oDiv.style.display="none";
            alert("批量生成完成");
            return;
          }
          iFrameUrl(arr[iNum]);
       }
  </script>

以上方法具有速度快,可以很好的控制格式(要先研究WORD转化为HTML后的格式),可以包含图片,可以批量导出的优点。 <script type="text/javascript"> window.document.οnkeydοwn=function() { if(window.event.keyCode==13) { var oLinkButton=document.getElementById('<%=this.LinkButton1.ClientID %>'); oLinkButton.click(); } } var iNum=0; var arr=new Array; function CheckAll(check, id) { var e = document.forms[0].elements; var l = e.length; var o; for(var i = 0; i < l; i++) { o = e[i]; if (o.type == "checkbox" && o.id.indexOf(id) > -1) { if(o.disabled!=true) { o.checked = check; } } } } function BatchExport() { var oDiv=window.document.getElementById("NoneDiv"); oDiv.style.display="block"; arr.splice(0,arr.length); iNum=0; var oCount=0; var oTable=window.document.getElementById('<%=this.CustDataGrid1.ClientID %>'); if(oTable!=null) { var oCheckbox=oTable.getElementsByTagName("INPUT"); for(var i=0;i 0) { oButton.οnclick=alertMessage; parent.wm(oButton); } else { oButton.οnclick=alertMessage; } } var odocName=null; var omessage=null; function alertMessage(obj) { word_onclick(); if(arguments.length==0) { if(odocName==null||odocName=="") { alert("导出失败!/r/n"+ omessage); } else { alert("导出成功!/r/n文件位置:C://" + odocName); } } } function getUrlParam(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; } function word_onclick(obj) { var oWD = new ActiveXObject("Word.Application"); oWD.WindowState = 2 var oDC = oWD.Documents.Add("",0,1); var oRange =oDC.Range(0,1); var hd = document.getElementById("__VIEWSTATE"); var hideTable = document.getElementById("hideTable"); var hideTable1 = document.getElementById("hideTable1"); var scriptHide = document.getElementById("scriptHide"); if(hd != null) { hd.parentNode.removeChild(hd); } if(hideTable != null) { hideTable.parentNode.removeChild(hideTable); } if(hideTable1 != null) { hideTable1.parentNode.removeChild(hideTable1); } if(scriptHide != null) { scriptHide.parentNode.removeChild(scriptHide); } var sel = document.body.createTextRange(); var lblName = document.getElementById("name"); var docName = "Q12 个人报告.doc"; var depno = '<%=Request.QueryString["DeptNo"].ToString()%>'; docName = depno +"-"+ docName; if(lblName != null) { docName = lblName.innerText.replace(//s*$/g,"")+"-"+ docName; } try { sel.execCommand("Copy"); oRange.Paste(); oDC.SaveAs("C://" + docName); odocName=docName; } catch(e) { omessage=e.message; } finally { oDC.Close(0); oWD.Quit(); oWD = null; } if(oWD != null) { oDC.Close(0); oWD.Quit(); oWD = null; } } </script> <script language="javascript" id="scriptHide" type="text/javascript"> window.οnlοad=function() { var oButton=window.document.getElementById("Button1"); if(oButton!=null && parent.iNum>0) { oButton.οnclick=alertMessage; parent.wm(oButton); } else { oButton.οnclick=alertMessage; } } var odocName=null; var omessage=null; function alertMessage(obj) { word_onclick(); if(arguments.length==0) { if(odocName==null||odocName=="") { alert("导出失败!/r/n"+ omessage); } else { alert("导出成功!/r/n文件位置:C://" + odocName); } } } function getUrlParam(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; } function word_onclick(obj) { var oWD = new ActiveXObject("Word.Application"); oWD.WindowState = 2 var oDC = oWD.Documents.Add("",0,1); var oRange =oDC.Range(0,1); var hd = document.getElementById("__VIEWSTATE"); var hideTable = document.getElementById("hideTable"); var hideTable1 = document.getElementById("hideTable1"); var scriptHide = document.getElementById("scriptHide"); if(hd != null) { hd.parentNode.removeChild(hd); } if(hideTable != null) { hideTable.parentNode.removeChild(hideTable); } if(hideTable1 != null) { hideTable1.parentNode.removeChild(hideTable1); } if(scriptHide != null) { scriptHide.parentNode.removeChild(scriptHide); } var sel = document.body.createTextRange(); var lblName = document.getElementById("name"); var docName = "Q12 个人报告.doc"; var depno = '<%=Request.QueryString["DeptNo"].ToString()%>'; docName = depno +"-"+ docName; if(lblName != null) { docName = lblName.innerText.replace(//s*$/g,"")+"-"+ docName; } try { sel.execCommand("Copy"); oRange.Paste(); oDC.SaveAs("C://" + docName); odocName=docName; } catch(e) { omessage=e.message; } finally { oDC.Close(0); oWD.Quit(); oWD = null; } if(oWD != null) { oDC.Close(0); oWD.Quit(); oWD = null; } } </script> <script language="javascript" id="scriptHide" type="text/javascript"> window.οnlοad=function() { var oButton=window.document.getElementById("Button1"); if(oButton!=null && parent.iNum>0) { oButton.οnclick=alertMessage; parent.wm(oButton); } else { oButton.οnclick=alertMessage; } } var odocName=null; var omessage=null; function alertMessage(obj) { word_onclick(); if(arguments.length==0) { if(odocName==null||odocName=="") { alert("导出失败!/r/n"+ omessage); } else { alert("导出成功!/r/n文件位置:C://" + odocName); } } } function getUrlParam(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; } function word_onclick(obj) { var oWD = new ActiveXObject("Word.Application"); oWD.WindowState = 2 var oDC = oWD.Documents.Add("",0,1); var oRange =oDC.Range(0,1); var hd = document.getElementById("__VIEWSTATE"); var hideTable = document.getElementById("hideTable"); var hideTable1 = document.getElementById("hideTable1"); var scriptHide = document.getElementById("scriptHide"); if(hd != null) { hd.parentNode.removeChild(hd); } if(hideTable != null) { hideTable.parentNode.removeChild(hideTable); } if(hideTable1 != null) { hideTable1.parentNode.removeChild(hideTable1); } if(scriptHide != null) { scriptHide.parentNode.removeChild(scriptHide); } var sel = document.body.createTextRange(); var lblName = document.getElementById("name"); var docName = "Q12 个人报告.doc"; var depno = '<%=Request.QueryString["DeptNo"].ToString()%>'; docName = depno +"-"+ docName; if(lblName != null) { docName = lblName.innerText.replace(//s*$/g,"")+"-"+ docName; } try { sel.execCommand("Copy"); oRange.Paste(); oDC.SaveAs("C://" + docName); odocName=docName; } catch(e) { omessage=e.message; } finally { oDC.Close(0); oWD.Quit(); oWD = null; } if(oWD != null) { oDC.Close(0); oWD.Quit(); oWD = null; } } </script> <script language="javascript" id="scriptHide" type="text/javascript"> window.οnlοad=function() { var oButton=window.document.getElementById("Button1"); if(oButton!=null && parent.iNum>0) { oButton.οnclick=alertMessage; parent.wm(oButton); } else { oButton.οnclick=alertMessage; } } var odocName=null; var omessage=null; function alertMessage(obj) { word_onclick(); if(arguments.length==0) { if(odocName==null||odocName=="") { alert("导出失败!/r/n"+ omessage); } else { alert("导出成功!/r/n文件位置:C://" + odocName); } } } function getUrlParam(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; } function word_onclick(obj) { var oWD = new ActiveXObject("Word.Application"); oWD.WindowState = 2 var oDC = oWD.Documents.Add("",0,1); var oRange =oDC.Range(0,1); var hd = document.getElementById("__VIEWSTATE"); var hideTable = document.getElementById("hideTable"); var hideTable1 = document.getElementById("hideTable1"); var scriptHide = document.getElementById("scriptHide"); if(hd != null) { hd.parentNode.removeChild(hd); } if(hideTable != null) { hideTable.parentNode.removeChild(hideTable); } if(hideTable1 != null) { hideTable1.parentNode.removeChild(hideTable1); } if(scriptHide != null) { scriptHide.parentNode.removeChild(scriptHide); } var sel = document.body.createTextRange(); var lblName = document.getElementById("name"); var docName = "Q12 个人报告.doc"; var depno = '<%=Request.QueryString["DeptNo"].ToString()%>'; docName = depno +"-"+ docName; if(lblName != null) { docName = lblName.innerText.replace(//s*$/g,"")+"-"+ docName; } try { sel.execCommand("Copy"); oRange.Paste(); oDC.SaveAs("C://" + docName); odocName=docName; } catch(e) { omessage=e.message; } finally { oDC.Close(0); oWD.Quit(); oWD = null; } if(oWD != null) { oDC.Close(0); oWD.Quit(); oWD = null; } } </script> <script language="javascript" id="scriptHide" type="text/javascript"> window.οnlοad=function() { var oButton=window.document.getElementById("Button1"); if(oButton!=null && parent.iNum>0) { oButton.οnclick=alertMessage; parent.wm(oButton); } else { oButton.οnclick=alertMessage; } } var odocName=null; var omessage=null; function alertMessage(obj) { word_onclick(); if(arguments.length==0) { if(odocName==null||odocName=="") { alert("导出失败!/r/n"+ omessage); } else { alert("导出成功!/r/n文件位置:C://" + odocName); } } } function getUrlParam(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; } function word_onclick(obj) { var oWD = new ActiveXObject("Word.Application"); oWD.WindowState = 2 var oDC = oWD.Documents.Add("",0,1); var oRange =oDC.Range(0,1); var hd = document.getElementById("__VIEWSTATE"); var hideTable = document.getElementById("hideTable"); var hideTable1 = document.getElementById("hideTable1"); var scriptHide = document.getElementById("scriptHide"); if(hd != null) { hd.parentNode.removeChild(hd); } if(hideTable != null) { hideTable.parentNode.removeChild(hideTable); } if(hideTable1 != null) { hideTable1.parentNode.removeChild(hideTable1); } if(scriptHide != null) { scriptHide.parentNode.removeChild(scriptHide); } var sel = document.body.createTextRange(); var lblName = document.getElementById("name"); var docName = "Q12 个人报告.doc"; var depno = '<%=Request.QueryString["DeptNo"].ToString()%>'; docName = depno +"-"+ docName; if(lblName != null) { docName = lblName.innerText.replace(//s*$/g,"")+"-"+ docName; } try { sel.execCommand("Copy"); oRange.Paste(); oDC.SaveAs("C://" + docName); odocName=docName; } catch(e) { omessage=e.message; } finally { oDC.Close(0); oWD.Quit(); oWD = null; } if(oWD != null) { oDC.Close(0); oWD.Quit(); oWD = null; } } </script> <script language="javascript" id="scriptHide" type="text/javascript"> window.οnlοad=function() { var oButton=window.document.getElementById("Button1"); if(oButton!=null && parent.iNum>0) { oButton.οnclick=alertMessage; parent.wm(oButton); } else { oButton.οnclick=alertMessage; } } var odocName=null; var omessage=null; function alertMessage(obj) { word_onclick(); if(arguments.length==0) { if(odocName==null||odocName=="") { alert("导出失败!/r/n"+ omessage); } else { alert("导出成功!/r/n文件位置:C://" + odocName); } } } function getUrlParam(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; } function word_onclick(obj) { var oWD = new ActiveXObject("Word.Application"); oWD.WindowState = 2 var oDC = oWD.Documents.Add("",0,1); var oRange =oDC.Range(0,1); var hd = document.getElementById("__VIEWSTATE"); var hideTable = document.getElementById("hideTable"); var hideTable1 = document.getElementById("hideTable1"); var scriptHide = document.getElementById("scriptHide"); if(hd != null) { hd.parentNode.removeChild(hd); } if(hideTable != null) { hideTable.parentNode.removeChild(hideTable); } if(hideTable1 != null) { hideTable1.parentNode.removeChild(hideTable1); } if(scriptHide != null) { scriptHide.parentNode.removeChild(scriptHide); } var sel = document.body.createTextRange(); var lblName = document.getElementById("name"); var docName = "Q12 个人报告.doc"; var depno = '<%=Request.QueryString["DeptNo"].ToString()%>'; docName = depno +"-"+ docName; if(lblName != null) { docName = lblName.innerText.replace(//s*$/g,"")+"-"+ docName; } try { sel.execCommand("Copy"); oRange.Paste(); oDC.SaveAs("C://" + docName); odocName=docName; } catch(e) { omessage=e.message; } finally { oDC.Close(0); oWD.Quit(); oWD = null; } if(oWD != null) { oDC.Close(0); oWD.Quit(); oWD = null; } } </script>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值