Internet explorer USES js to export web pages to excel and print

  • 2020-03-30 02:20:57
  • OfStack

In small, less demanding projects, you can implement functionality using technologies that are not universal or new, but that do work well. This system is not very complex to display, and can be easy to maintain.  
Create a new exportprint.html page, the code in the following, you can export to Excel and print pages.


<html>
 <head>
  <title>IE Browser usage JS technology Export to Excel And print </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">
      The user information 
    </td>
   </tr>
   <tr>
    <td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="1">
      The name 
    </td>
    <td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="1">
      Zhang SAN 
    </td>
   </tr>

   <tr>
    <td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="2">
     <input type="button" id="export" value=" export " onclick="javascript:exportToExcel();" >
     <input type="button" id="print" value=" print " onclick="javascript:print();" >
    </td>
   </tr>
  </table>
 </body>
</html>
<script type="text/javaScript">
 //Export to 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(" Error! Maybe it's the browser or too much data! ");
    return;
   }
   appExcel = null;
   oRangeRef = null;
  }
 }

 // print 
 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(" Error! Maybe it's the browser or too much data! ");
    return;
   }
   export.style.display = "";
   print.style.display = "";
  }
 }
</script>


Related articles: