zoukankan      html  css  js  c++  java
  • 前端导出&配置问题

    <button class="search" onclick="method5('dataTable');">导出</button> 在表格内需要绑定相对应的id

    js操作

      1 var idTmr;
      2 
      3 function getExplorer() {
      4     var explorer = window.navigator.userAgent;
      5     //ie  
      6     if(explorer.indexOf("MSIE") >= 0) {
      7         return 'ie';
      8     }
      9     //firefox  
     10     else if(explorer.indexOf("Firefox") >= 0) {
     11         return 'Firefox';
     12     }
     13     //Chrome  
     14     else if(explorer.indexOf("Chrome") >= 0) {
     15         return 'Chrome';
     16     }
     17     //Opera  
     18     else if(explorer.indexOf("Opera") >= 0) {
     19         return 'Opera';
     20     }
     21     //Safari  
     22     else if(explorer.indexOf("Safari") >= 0) {
     23         return 'Safari';
     24     }
     25 }
     26 
     27 function method5(tableid) {
     28     console.log(tableid)
     29     if(getExplorer() == 'ie') {
     30         var curTbl = document.getElementById(tableid);
     31         var oXL = new ActiveXObject("Excel.Application");
     32         var oWB = oXL.Workbooks.Add();
     33         var xlsheet = oWB.Worksheets(1);
     34         var sel = document.body.createTextRange();
     35         sel.moveToElementText(curTbl);
     36         sel.select();
     37         sel.execCommand("Copy");
     38         xlsheet.Paste();
     39         oXL.Visible = true;
     40 
     41         try {
     42             var fname = oXL.Application.GetSaveAsFilename("Excel.xls",
     43                 "Excel Spreadsheets (*.xls), *.xls");
     44         } catch(e) {
     45             print("Nested catch caught " + e);
     46         } finally {
     47             oWB.SaveAs(fname);
     48             oWB.Close(savechanges = false);
     49             oXL.Quit();
     50             oXL = null;
     51             idTmr = window.setInterval("Cleanup();", 1);
     52         }
     53 
     54     } else {
     55         tableToExcel(tableid)
     56     }
     57 }
     58 
     59 function Cleanup() {
     60     window.clearInterval(idTmr);
     61     CollectGarbage();
     62 }
     63 var tableToExcel = (function() {
     64     var uri = 'data:application/vnd.ms-excel;base64,',
     65         template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"'+
     66                                 'xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>'
     67                                 +'<x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets>'
     68                                 +'</x:ExcelWorkbook></xml><![endif]-->'+
     69                                 ' <style type="text/css">'+
     70                                 '.excelTable  {'+
     71                                 'border-collapse:collapse;'+
     72                                  ' border:thin solid #999; '+
     73                                 '}'+
     74                                 '   .excelTable  th {'+
     75                                 '   border: thin solid #999;'+
     76                                 '  padding:20px;'+
     77                                 '  text-align: center;'+
     78                                 '  border-top: thin solid #999;'+
     79                                 ' background-color: #E6E6E6;'+
     80                                 ' }'+
     81                                 ' .excelTable  td{'+
     82                                 ' border:thin solid #999;'+
     83                                 '  padding:2px 5px;'+
     84                                 '  text-align: center;'+
     85                                 ' }</style>'+
     86                                 '</head><body ><table class="excelTable">{table}</table></body></html>',
     87 
     88         base64 = function(
     89             s) {
     90             return window.btoa(unescape(encodeURIComponent(s)))
     91         },
     92         format = function(s, c) {
     93             return s.replace(/{(w+)}/g, function(m, p) {
     94                 return c[p];
     95             })
     96         }
     97     return function(table, name) {
     98         console.log(table)
     99         if(!table.nodeType)
    100             table = document.getElementById(table)
    101         var ctx = {
    102             worksheet: name || 'Worksheet',
    103             table: table.innerHTML
    104         }
    105         var link = document.createElement("a");
    106         link.href = uri+base64(format(template, ctx));
    107         link.download = "信息发布情况表.xls";//当前下载的excel名称
    108         document.body.appendChild(link);
    109         link.click();
    110         document.body.removeChild(link);
    111     }
    112 })()
     






  • 相关阅读:
    Struts2
    Struts2
    学习python的第九天
    学习python的第八天--作业
    学习python的第七天--作业
    学习python第六天 --作业
    学习python的第六天---1(理论)
    学习python第五天
    学习python第四天
    学习python第三天
  • 原文地址:https://www.cnblogs.com/suichenming/p/10917077.html
Copyright © 2011-2022 走看看