save table

help someone save html table to xls easy~

当前为 2022-08-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name save table
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @match http://fvp-showtool.sf-express.com/*
  6. // @match http://fvp-showtool.sit.sf-express.com/*
  7. // @description help someone save html table to xls easy~
  8. // @author vector
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12. (function () {
  13.  
  14. var tableId = "hello"
  15.  
  16. function HtmlExportToExcel(tableid, filename) {
  17. if (getExplorer() === 'ie' || getExplorer() === undefined) {
  18. console.warn("自动保存xls暂不支持IE");
  19. } else {
  20. HtmlExportToExcelForEntire(tableid, filename)
  21. }
  22. }
  23.  
  24. function getExplorer() {
  25. var explorer = window.navigator.userAgent;
  26. //ie
  27. if (explorer.indexOf("MSIE") >= 0) {
  28. return 'ie';
  29. }
  30. //firefox
  31. else if (explorer.indexOf("Firefox") >= 0) {
  32. return 'Firefox';
  33. }
  34. //Chrome
  35. else if (explorer.indexOf("Chrome") >= 0) {
  36. return 'Chrome';
  37. }
  38. //Opera
  39. else if (explorer.indexOf("Opera") >= 0) {
  40. return 'Opera';
  41. }
  42. //Safari
  43. else if (explorer.indexOf("Safari") >= 0) {
  44. return 'Safari';
  45. }
  46. }
  47.  
  48.  
  49.  
  50. var HtmlExportToExcelForEntire = (function () {
  51. var uri = 'data:application/vnd.ms-excel;base64,',
  52. 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><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><!--[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]--><style>table td {font-size: 12px;width: 200px;height: 30px;text-align: center;border:1px 1px;}</style></head><body><table border="1">{table}</table></body></html>',
  53. base64 = function (s) {
  54. return window.btoa(unescape(encodeURIComponent(s)))
  55. },
  56. format = function (s, c) {
  57. return s.replace(/{(\w+)}/g, function (m, p) {
  58. return c[p];
  59. })
  60. }
  61. return function (table, name) {
  62. if (!table.nodeType) {
  63. table = document.getElementById(table);
  64. }
  65. // table.style='mso-number-format:@'
  66. fillt(table);
  67.  
  68. var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
  69. var a = document.createElement("a");
  70. a.href = uri + base64(format(template, ctx));
  71. a.download = name + ".xls";
  72. a.click();
  73. }
  74. })()
  75.  
  76. function fillt(table) {
  77.  
  78. var tds = table.getElementsByTagName("td")
  79. for (let i = 0; i < tds.length; i++) {
  80. tds[i].innerText = '`'+tds[i].innerText
  81. // tds[i].style="mso-number-format: @;"
  82. // tds[i].style='margin: 0 8px;color:blue;cursor:pointer;'
  83. }
  84. }
  85.  
  86.  
  87. function genBtn() {
  88. var btn = document.createElement('input');
  89. btn.className = 'size2';
  90. btn.style = 'margin-left: 10px; background-color: #73bcfc';
  91. btn.value = '下载到本地';
  92. btn.type = "button";
  93. btn.onclick = function () {
  94. var input = document.querySelector("#FactRouteAllTrack_waybillNo")
  95. var wo = input.value; // 单号
  96. var type = document.querySelector("#type").value;
  97.  
  98. var xlsname;
  99. if (!wo) {
  100. window.confirm("请输入单号查询");
  101. return;
  102. }
  103. xlsname = wo;
  104. if (type) {
  105. xlsname = wo + '_' + type;
  106. }
  107. //下载整个表格
  108. HtmlExportToExcel(tableId, xlsname);
  109. };
  110. return btn;
  111. }
  112.  
  113. function createDownloadBtn() {
  114. var dom = document.querySelector("#query fieldset");
  115.  
  116. dom?.lastChild.appendChild(genBtn());
  117. }
  118.  
  119. function guid() {
  120. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  121. var r = Math.random() * 16 | 0,
  122. v = c === 'x' ? r : (r & 0x3 | 0x8);
  123. return v.toString(16);
  124. });
  125. }
  126.  
  127. setTimeout(function () {
  128. createDownloadBtn()
  129. }, 1000);
  130.  
  131. // createDownloadBtn();
  132. })();