var idTmr; function getExplorer() { var explorer = window.navigator.userAgent; //ie if (explorer.indexOf("MSIE") >= 0) { return 'ie'; } //firefox else if (explorer.indexOf("Firefox") >= 0) { return 'Firefox'; } //Chrome else if (explorer.indexOf("Chrome") >= 0) { return 'Chrome'; } //Opera else if (explorer.indexOf("Opera") >= 0) { return 'Opera'; } //Safari else if (explorer.indexOf("Safari") >= 0) { return 'Safari'; } } function ExportExcel(tableid, name,style) {//整个表格拷贝到EXCEL中 if (getExplorer() == 'ie') { var curTbl = document.getElementById(tableid); var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel var oWB = oXL.Workbooks.Add(); //获取workbook对象 var xlsheet = oWB.Worksheets(1); //激活当前sheet var sel = document.body.createTextRange(); sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中 sel.select; //全选TextRange中内容 sel.execCommand("Copy"); //复制TextRange中内容 xlsheet.Paste(); //粘贴到活动的EXCEL中 oXL.Visible = true; //设置excel可见属性 try { var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls"); } catch (e) { print("Nested catch caught " + e); } finally { oWB.SaveAs(fname); oWB.Close(savechanges = false); //xls.visible = false; oXL.Quit(); oXL = null; //结束excel进程,退出完成 //window.setInterval("Cleanup();",1); idTmr = window.setInterval("Cleanup();", 1); } } else { //tableToExcel(tableid, name, style); tableToExcel2(tableid, name, style); } } function Cleanup() { window.clearInterval(idTmr); CollectGarbage(); } /* template : 定义文档的类型,相当于html页面中顶部的 声明。(个人理解,不确定) encodeURIComponent:解码 unescape() 函数:对通过 escape() 编码的字符串进行解码。 window.btoa(window.encodeURIComponent(str)):支持汉字进行解码。 \w :匹配包括下划线的任何单词字符。等价于’[A-Za-z0-9_]’ replace()方法:用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。 {(\w+)}:匹配所有 {1个或更多字符} 形式的字符串;此处匹配输出内容是 “worksheet” 正则中的() :是为了提取匹配的字符串。表达式中有几个()就有几个相应的匹配字符串。 讲解(/{(\w+)}/g, function(m, p) { return c[p]; } : /{(\w+)}/g 匹配出所有形式为“{worksheet}”的字符串; function参数: m 正则所匹配到的内容,即“worksheet”; p 正则表达式中分组的内容,即“(\w+)”分组中匹配到的内容,为“worksheet”; c :为object,见下图3 c[p] : 为“worksheet” */ var tableToExcel = (function () { var uri = 'data:application/vnd.ms-excel;base64,', template = '
{style}