前端导出&配置问题
程序员文章站
2022-04-15 15:43:52
js操作 ......
<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 })()