zl程序教程

您现在的位置是:首页 >  工具

当前栏目

ie浏览器使用js导出网页到excel并打印

ExcelJS导出网页浏览器 使用 IE 并打印
2023-06-13 09:15:19 时间

在一些要求不是很高的小项目中,可以使用一些虽不是通用且不是新技术但是确实可以很好实现功能的技术来实现这些功能。这样系统不是显示的很复杂,且可以方便维护。 
新建一个exportPrint.html页面,里面的代码如下所示,就可以实现导出到Excel和打印网页。

复制代码代码如下:


<html>
 <head>
  <title>IE浏览器使用JS技术导出到Excel和打印</title>
  <style>
   .table_stat{
    border-right:0px;
    border-bottom:0px;
    border-left:1pxsolid#819BD8;
    border-top:1pxsolid#819BD8;
   }
   .td_stat{
    border-right:1pxsolid#819BD8;
    border-bottom:1pxsolid#819BD8;
   }
  </style>
 </head>
 <body>
  <objectclassid="CLSID:8856F961-340A-11DO-A96B-00C04FD705A2"height="0"id="WebBrowser"width="0"></object>
  <tablewidth="100%"align="center"border="0"cellpadding="0"cellspacing="0"style="text-align:center;"class="table_stat">
   <tr>
    <tdid="title"align="center"nowrap="nowrap"class="td_stat"colspan="2">
     用户信息
    </td>
   </tr>
   <tr>
    <tdid="title"align="center"nowrap="nowrap"class="td_stat"colspan="1">
     姓名
    </td>
    <tdid="title"align="center"nowrap="nowrap"class="td_stat"colspan="1">
     张三
    </td>
   </tr>

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

<scripttype="text/javaScript">
 //导出到Excel
 functionexportToExcel(){
  if(document.getElementById("title")){
   try{
    varoRangeRef=document.body.createTextRange();
    oRangeRef.execCommand("Copy");
    varappExcel=newActiveXObject("Excel.Application");
    appExcel.visible=true;
    appExcel.Workbooks.Add().WorkSheets.Item(1).Paste();
   }catch(e){
    alert("出错啦!可能是浏览器或者是数据量太大咯哦!");
    return;
   }
   appExcel=null;
   oRangeRef=null;
  }
 }

 //打印
 functionprint(){
  if(document.getElementById("title")){
   varexport=document.getElementById("export");
   varprint=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>