从安
2019-06-21
来源 :
阅读 1528
评论 0
摘要:本篇文章主要讲述JavaScript之轻松解析前端导出&配置问题,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入。
本篇文章主要讲述JavaScript之轻松解析前端导出&配置问题,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入。

<button class="search" onclick="method
5('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="//www.w3.org/TR/REC-html40"><head><!--[if gtemso9]><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>{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 })()本文由职坐标整理发布,学习更多的相关知识,请关注职坐标IT知识库!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号