使用js代码将html导出为Excel

前端开发   发布日期:2025年05月22日   浏览次数:155

js代码将html导出为Excel的方法:

直接上源码:

  1. <script type="text/javascript" language="javascript">
  2. var idTmr;
  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. function method1(tableid) {//整个表格拷贝到EXCEL中
  27. if(getExplorer()=='ie')
  28. {
  29. var curTbl = document.getElementById(tableid);
  30. var oXL = new ActiveXObject("Excel.Application");
  31. //创建AX对象excel
  32. var oWB = oXL.Workbooks.Add();
  33. //获取workbook对象
  34. var xlsheet = oWB.Worksheets(1);
  35. //激活当前sheet
  36. var sel = document.body.createTextRange();
  37. sel.moveToElementText(curTbl);
  38. //把表格中的内容移到TextRange中
  39. sel.select();
  40. //全选TextRange中内容
  41. sel.execCommand("Copy");
  42. //复制TextRange中内容
  43. xlsheet.Paste();
  44. //粘贴到活动的EXCEL中
  45. oXL.Visible = true;
  46. //设置excel可见属性
  47. try {
  48. var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
  49. } catch (e) {
  50. print("Nested catch caught " + e);
  51. } finally {
  52. oWB.SaveAs(fname);
  53. oWB.Close(savechanges = false);
  54. //xls.visible = false;
  55. oXL.Quit();
  56. oXL = null;
  57. //结束excel进程,退出完成
  58. //window.setInterval("Cleanup();",1);
  59. idTmr = window.setInterval("Cleanup();", 1);
  60. }
  61. }
  62. else
  63. {
  64. tableToExcel(tableid)
  65. }
  66. }
  67. function Cleanup() {
  68. window.clearInterval(idTmr);
  69. CollectGarbage();
  70. }
  71. var tableToExcel = (function() {
  72. var uri = 'data:application/vnd.ms-excel;base64,',
  73. 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"><meta http-equiv="Content-Type" charset=utf-8"><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]--></head><body></p>
  74. <table>{table}</table>
  75. <p></body></html>',
  76. base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
  77. format = function(s, c) {
  78. return s.replace(/{(\w+)}/g,
  79. function(m, p) { return c[p]; }) }
  80. return function(table, name) {
  81. if (!table.nodeType) table = document.getElementById(table)
  82. var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
  83. window.location.href = uri + base64(format(template, ctx))
  84. }
  85. })()
  86. </script>
  87. </pre>
  88. <table >
  89. <tbody>
  90. <tr>
  91. <th width="10%">调查案例</th>
  92. <th width="10%">公司名称</th>
  93. <th width="10%">地  址</th>
  94. <th width="5%">部门</th>
  95. <th width="5%">联系人</th>
  96. <th width="10%">联系方式</th>
  97. <th width="20%">具體情况</th>
  98. <th width="20%">问券填写</th>
  99. <th width="10%">提交时间</th>
  100. </tr>
  101. </tbody>
  102. </table>
  103. <input />
  104. <pre>

  

以上就是使用js代码将html导出为Excel的详细内容,更多关于使用js代码将html导出为Excel的资料请关注九品源码其它相关文章!