ie瀏覽器使用js匯出網頁到excel並列印

來源:互聯網
上載者:User

在一些要求不是很高的小項目中,可以使用一些雖不是通用且不是新技術但是確實可以很好實現功能的技術來實現這些功能。這樣系統不是顯示的很複雜,且可以方便維護。 
建立一個exportPrint.html頁面,裡面的代碼如下所示,就可以實現匯出到Excel和列印網頁。

複製代碼 代碼如下:
<html>
 <head>
  <title>IE瀏覽器使用JS技術匯出到Excel和列印</title>
  <style>
   .table_stat {
    border-right:0px;
    border-bottom:0px;
    border-left:1px solid #819BD8;
    border-top:1px solid #819BD8;
   }
   .td_stat {
    border-right:1px solid #819BD8;
    border-bottom:1px solid #819BD8;
   }
  </style>
 </head>
 <body>
  <object classid="CLSID:8856F961-340A-11DO-A96B-00C04FD705A2" height="0" id="WebBrowser" width="0"></object>
  <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" style="text-align: center;" class="table_stat">
   <tr>
    <td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="2">
     使用者資訊
    </td>
   </tr>
   <tr>
    <td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="1">
     姓名
    </td>
    <td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="1">
     張三
    </td>
   </tr>

   <tr>
    <td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="2">
     <input type="button" id="export" value="匯出" onclick="javascript:exportToExcel();" >
     <input type="button" id="print" value="列印" onclick="javascript:print();" >
    </td>
   </tr>
  </table>
 </body>
</html>

<script type="text/javaScript">
 //匯出到Excel
 function exportToExcel() {
  if(document.getElementById("title")) {
   try {
    var oRangeRef = document.body.createTextRange();
    oRangeRef.execCommand("Copy");
    var appExcel = new ActiveXObject("Excel.Application");
    appExcel.visible = true;
    appExcel.Workbooks.Add().WorkSheets.Item(1).Paste();
   } catch(e) {
    alert("出錯啦!可能是瀏覽器或者是資料量太大咯哦!");
    return;
   }
   appExcel = null;
   oRangeRef = null;
  }
 }

 //列印
 function print() {
  if(document.getElementById("title")) {
   var export = document.getElementById("export");
   var print = document.getElementById("print");
   try {
    export.style.display = "none";
    print.style.display = "none";
    document.all.WebBrowser.ExecWB(6,1);
   } catch(e) {
    alert("出錯啦!可能是瀏覽器或者是資料量太大咯哦!");
    return;
   }
   export.style.display = "";
   print.style.display = "";
  }
 }
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.