用JS指令碼大量匯出WORD的方法(可以有圖片)

來源:互聯網
上載者:User

 該方法適用以下情況:一個列表,每一行有串連,點擊可以進入另外的頁面,列表旁可以放選擇按鈕,如果選擇了,就匯出該行串連頁面。

該方法注意事項:1,匯出的頁面,CSS格式要讓WORD可以識別。最好現在WORD上把頁面畫出,然後另存新檔HTML檔案,注意儲存其中產生的檔案。

2,瀏覽器要准許ActiveX運行。

代碼如下:

1,以下代碼即可以單獨匯出一個頁面的WORD文檔。

<script id="scriptHide" language="javascript">
window.onload=function()
{
    var oButton=window.document.getElementById("Button1");
    if(oButton!=null && parent.iNum>0)
    {
           oButton.onclick=alertMessage;
           parent.wm(oButton);
    }
     else
    {
           oButton.onclick=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.onkeydown=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後的格式),可以包含圖片,可以大量匯出的優點。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.