- 1 /*
- 2 * HTML导出Excel文件(兼容IE及所有浏览器)
- 3 * @param {any} tableid table父元素ID
- 4 * @param {any} filename 文件名称
- 5 */
- 6 function HtmlExportToExcel(tableid, filename) {
- 7 if (getExplorer() === 'ie' || getExplorer() === undefined) {
- 8 HtmlExportToExcelForIE(tableid, filename);
- 9 }
- 10 else {
- 11 HtmlExportToExcelForEntire(tableid, filename);
- 12 }
- 13 }
- 14
- 15 //IE浏览器导出Excel
- 16 function HtmlExportToExcelForIE(tableId, filename) {
- 17 try {
- 18 var oXL = new ActiveXObject("excel.Application");
- 19 //oXL.Visible = true;
- 20 //oXL.ScreenUpdating = false;
- 21 } catch (e1) {
- 22 try {
- 23 oXL = new ActiveXObject("et.Application");
- 24 } catch (e2) {
- 25 alert(e2.description + "\n\n\n要使用EXCEL对象,您必须安装Excel电子表格软件\n或者,需要安装Kingsoft ET软件\n\n同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。");
- 26 return;
- 27 }
- 28 }
- 29 //创建AX对象excel
- 30 var oWB = oXL.Workbooks.Add();
- 31 //获取workbook对象
- 32 var xlsheet = oWB.Worksheets(1);
- 33
- 34 var elTable = document.getElementById(tableId);
- 35
- 36 //替换掉表格td中隐藏的html元素
- 37 var tableHtml = ReplaceHtml(elTable.innerHTML);
- 38
- 39 var newTable = document.getElementById("newData");
- 40 //console.log();
- 41 newTable.innerHTML = tableHtml;
- 42
- 43 //激活当前sheet
- 44 var sel = document.body.createTextRange();
- 45 sel.moveToElementText(newTable);
- 46 //把表格中的内容移到TextRange中
- 47 sel.select;
- 48 //全选TextRange中内容
- 49 sel.execCommand("Copy");
- 50 //复制TextRange中内容
- 51 xlsheet.Paste();
- 52 //粘贴到活动的EXCEL中
- 53 oXL.Visible = true;
- 54 //设置excel可见属性
- 55
- 56 newTable.innerHTML = "";
- 57
- 58 try {
- 59 //设置 sheet 名称
- 60 xlsheet.Name = filename;
- 61 var fname = oXL.Application.GetSaveAsFilename(filename + ".xls", "Excel Spreadsheets (*.xls), *.xls");
- 62 } catch (e) {
- 63 print("Nested catch caught " + e);
- 64 } finally {
- 65 oWB.SaveAs(fname);
- 66 oWB.Close();
- 67 //xls.visible = false;
- 68 oXL.ScreenUpdating = true;
- 69 oXL.Quit();
- 70 }
- 71 }
- 72
- 73 //非IE浏览器导出Excel
- 74 var HtmlExportToExcelForEntire = (function () {
- 75 var uri = 'data:application/vnd.ms-excel;base64,',
- 76 template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->' +
- 77 /**********这部分是加载表格的样式 没有样式可以省略 start**********/
- 78 '<style type="text/css">' +
- 79 '.tablefrom {width: 100%;border-collapse: collapse;}' +
- 80 '.tablefrom, .tablefrom td, .tablefrom th {text-align: center;font: 12px Arial, Helvetica, sans-serif;border: 1px solid #fff;}' +
- 81 '.tablefrom th{background:#328aa4;color:#fff;}' +
- 82 '.tablefrom td{background:#e5f1f4;}' +
- 83 '.tablefrom .BlueBgColor td {color: #fff;background-color: #0070c0;}' +
- 84 '.tablefrom .LightBlueBgColor td {color: #000000;background-color: #bdd7ee;}' +
- 85 '.tablefrom tr .BlueBgColorTd { color: #fff;background-color: #0070c0;}' +
- 86 '.tablefrom tr .LightBlueBgColorTd {color: #000000;background-color: #bdd7ee;}' +
- 87 '</style>'
- 88 /**********这部分是加载表格的样式 没有样式可以省略 end**********/
- 89 + '</head><body>{table}</body></html>',
- 90 base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))); },
- 91 format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }); };
- 92 return function (table, name) {
- 93 if (!table.nodeType) { table = document.getElementById(table); }
- 94 //替换掉表格td中隐藏的html元素
- 95 var strHTML = ReplaceHtml(table.innerHTML);
- 96 var ctx = { worksheet: name || 'Worksheet', table: strHTML };
- 97
- 98 document.getElementById("dlink").href = uri + base64(format(template, ctx));
- 99 document.getElementById("dlink").download = name + ".xls";
- 100 document.getElementById("dlink").click();
- 101 };
- 102 })();
- 103
- 104 //获取当前使用浏览器
- 105 function getExplorer() {
- 106 var explorer = window.navigator.userAgent;
- 107 //ie
- 108 if (explorer.indexOf("MSIE") >= 0) {
- 109 return 'ie';
- 110 }
- 111 //firefox
- 112 else if (explorer.indexOf("Firefox") >= 0) {
- 113 return 'Firefox';
- 114 }
- 115 //Chrome
- 116 else if (explorer.indexOf("Chrome") >= 0) {
- 117 return 'Chrome';
- 118 }
- 119 //Opera
- 120 else if (explorer.indexOf("Opera") >= 0) {
- 121 return 'Opera';
- 122 }
- 123 //Safari
- 124 else if (explorer.indexOf("Safari") >= 0) {
- 125 return 'Safari';
- 126 }
- 127 }
- 128
- 129 //将隐藏的HTML元素替换掉
- 130 function ReplaceHtml(tableHtml) {
- 131 var radioValue = $('input[name="bedStatus"]:checked ').val();
- 132 if (radioValue === 'yuan') {
- 133 tableHtml = tableHtml.replace(/<span class="span_wanyuan" [^<>]*?>(.*?)<\/span>/gi, "");
- 134 }
- 135 else if (radioValue === 'wanyuan') {
- 136 tableHtml = tableHtml.replace(/<span class="span_yuan" [^<>]*?>(.*?)<\/span>/gi, "");
- 137 }
- 138 return tableHtml;
- 139 }