经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JSJS库框架 » JavaScript » 查看文章
HTML导出Excel文件(兼容IE及所有浏览器)
来源:cnblogs  作者:£冷☆月№  时间:2018/9/26 17:54:21  对本文有异议

注意:IE浏览器需要以下设置:

打开IE,在常用工具栏中选择“工具”--->Internet选项---->选择"安全"标签页--->选择"自定义级别"--->弹出的窗口中找到ActiveX控件和插件--->
将"ActiveX控件自动提示"
"对没有标记安全的ActiveX控件进行初始化和脚本运行"
"下载未签名的ActiveX控件"
三项 选择启用 即可!

HtmlExportToExcel.js 代码

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

另外,我的功能需要替换掉td中隐藏的html元素,所以需要将新的table复制到另一个隐藏的div中。

页面html代码:

  1. 1 <div id="divData">
  2. 2 <table class="tablefrom" id="tbData" style="width: 100%; border-collapse: collapse;" border="0" cellspacing="0">
  3. 3 <tbody>
  4. 4 <tr>
  5. 5 <th rowspan="2" colspan="2" scope="col">序号</th>
  6. 6 <th style="min-width: 70px;" rowspan="2" scope="col">项目</th>
  7. 7 <th style="min-width: 50px;" rowspan="2" scope="col">维度</th>
  8. 8 <th style="min-width: 50px;" rowspan="2" scope="col">本月资金</th>
  9. 9 <th rowspan="2" colspan="1" scope="col">某部门</th>
  10. 10 <th colspan="2" scope="col">支付</th>
  11. 11 </tr>
  12. 12 <tr>
  13. 13 <th style="min-width: 50px;" scope="col">执行</th>
  14. 14 <th style="min-width: 50px;" scope="col">余额</th>
  15. 15 </tr>
  16. 16 <tr>
  17. 17 <td style="color: rgb(0, 0, 0); font-weight: bold; background-color: rgb(189, 215, 238);" rowspan="163" scope="col">经营费用类</td>
  18. 18 <td style="min-width: 50px;" rowspan="3" scope="col">1</td>
  19. 19 <td rowspan="3" scope="col">销售退款</td>
  20. 20 <td scope="col">实际</td>
  21. 21 <td style="color: rgb(0, 0, 0); background-color: rgb(189, 215, 238);" scope="col"><span class="span_yuan">100000</span><span class="span_wanyuan" style="display: none;">10</span></td>
  22. 22 <td scope="col"><span class="span_yuan">100000</span><span class="span_wanyuan" style="display: none;">10</span></td>
  23. 23 <td scope="col"><span class="span_yuan">100000</span><span class="span_wanyuan" style="display: none;">10</span></td>
  24. 24 <td scope="col"><span class="span_yuan">900000</span><span class="span_wanyuan" style="display: none;">90</span></td>
  25. 25 </tr>
  26. 26 <tr>
  27. 27 <td scope="col">计划</td>
  28. 28 <td style="color: rgb(0, 0, 0); background-color: rgb(189, 215, 238);" scope="col"><span class="span_yuan">1000000</span><span class="span_wanyuan" style="display: none;">100</span></td>
  29. 29 <td scope="col"><span class="span_yuan">1000000</span><span class="span_wanyuan" style="display: none;">100</span></td>
  30. 30 <td scope="col">-</td>
  31. 31 <td scope="col">-</td>
  32. 32 </tr>
  33. 33 <tr>
  34. 34 <td scope="col">差额</td>
  35. 35 <td style="color: rgb(0, 0, 0); background-color: rgb(189, 215, 238);" scope="col"><span class="span_yuan">900000</span><span class="span_wanyuan" style="display: none;">90</span></td>
  36. 36 <td scope="col"><span class="span_yuan">900000</span><span class="span_wanyuan" style="display: none;">90</span></td>
  37. 37 <td scope="col">-</td>
  38. 38 <td scope="col">-</td>
  39. 39 </tr>
  40. 40 </tbody>
  41. 41 </table>
  42. 42 </div>
  43. 43 <div id="newData" style="display: none;"></div>
View Code
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号