商丘12345 前端

jquery.table2excel.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * jQuery table2excel - v1.0.2
  3. * jQuery plugin to export an .xls file in browser from an HTML table
  4. * https://github.com/rainabba/jquery-table2excel
  5. *
  6. * Made by rainabba
  7. * Under MIT License
  8. */
  9. //table2excel.js
  10. ;(function ( $, window, document, undefined ) {
  11. var pluginName = "table2excel",
  12. defaults = {
  13. exclude: ".noExl",
  14. name: "Table2Excel"
  15. };
  16. // The actual plugin constructor
  17. function Plugin ( element, options ) {
  18. this.element = element;
  19. // jQuery has an extend method which merges the contents of two or
  20. // more objects, storing the result in the first object. The first object
  21. // is generally empty as we don't want to alter the default options for
  22. // future instances of the plugin
  23. //
  24. this.settings = $.extend( {}, defaults, options );
  25. this._defaults = defaults;
  26. this._name = pluginName;
  27. this.init();
  28. }
  29. Plugin.prototype = {
  30. init: function () {
  31. var e = this;
  32. e.template = {
  33. head: "<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 charset=\"UTF-8\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",
  34. sheet: {
  35. head: "<x:ExcelWorksheet><x:Name>",
  36. tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
  37. },
  38. mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",
  39. table: {
  40. head: "<table>",
  41. tail: "</table>"
  42. },
  43. foot: "</body></html>"
  44. };
  45. e.tableRows = [];
  46. // get contents of table except for exclude
  47. $(e.element).each( function(i,o) {
  48. var tempRows = "";
  49. $(o).find("tr").not(e.settings.exclude).each(function (i,o) {
  50. tempRows += "<tr>" + $(o).html() + "</tr>";
  51. });
  52. e.tableRows.push(tempRows);
  53. });
  54. // exclude img tags
  55. if(e.settings.exclude_img) {
  56. e.tableRows[0] = exclude_img(e.tableRows[0]);
  57. }
  58. // exclude link tags
  59. if(e.settings.exclude_links) {
  60. e.tableRows[0] = exclude_links(e.tableRows[0]);
  61. }
  62. // exclude input tags
  63. if(e.settings.exclude_inputs) {
  64. e.tableRows[0] = exclude_inputs(e.tableRows[0])
  65. }
  66. e.tableToExcel(e.tableRows, e.settings.name);
  67. },
  68. tableToExcel: function (table, name) {
  69. var e = this, fullTemplate="", i, link, a;
  70. e.uri = "data:application/vnd.ms-excel;base64,";
  71. e.base64 = function (s) {
  72. return window.btoa(unescape(encodeURIComponent(s)));
  73. };
  74. e.format = function (s, c) {
  75. return s.replace(/{(\w+)}/g, function (m, p) {
  76. return c[p];
  77. });
  78. };
  79. e.ctx = {
  80. worksheet: name || "Worksheet",
  81. table: table
  82. };
  83. fullTemplate= e.template.head;
  84. if ( $.isArray(table) ) {
  85. for (i in table) {
  86. //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;
  87. fullTemplate += e.template.sheet.head + "Table" + i + "" + e.template.sheet.tail;
  88. }
  89. }
  90. fullTemplate += e.template.mid;
  91. if ( $.isArray(table) ) {
  92. for (i in table) {
  93. fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
  94. }
  95. }
  96. fullTemplate += e.template.foot;
  97. for (i in table) {
  98. e.ctx["table" + i] = table[i];
  99. }
  100. delete e.ctx.table;
  101. if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
  102. {
  103. if (typeof Blob !== "undefined") {
  104. //use blobs if we can
  105. fullTemplate = [fullTemplate];
  106. //convert to array
  107. var blob1 = new Blob(fullTemplate, { type: "text/html" });
  108. window.navigator.msSaveBlob(blob1, getFileName(e.settings) );
  109. } else {
  110. //otherwise use the iframe and save
  111. //requires a blank iframe on page called txtArea1
  112. txtArea1.document.open("text/html", "replace");
  113. txtArea1.document.write(fullTemplate);
  114. txtArea1.document.close();
  115. txtArea1.focus();
  116. sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings) );
  117. }
  118. } else {
  119. link = e.uri + e.base64(e.format(fullTemplate, e.ctx));
  120. a = document.createElement("a");
  121. a.download = getFileName(e.settings);
  122. a.href = link;
  123. a.click();
  124. }
  125. return true;
  126. }
  127. };
  128. function getFileName(settings) {
  129. return ( settings.filename ? settings.filename : "table2excel") + ".xls";
  130. }
  131. // Removes all img tags
  132. function exclude_img(string) {
  133. return string.replace(/<img[^>]*>/gi,"");
  134. }
  135. // Removes all link tags
  136. function exclude_links(string) {
  137. return string.replace(/<A[^>]*>|<\/A>/g, "");
  138. }
  139. // Removes input params
  140. function exclude_inputs(string) {
  141. return string.replace(/<input[^>]*>|<\/input>/gi, "");
  142. }
  143. $.fn[ pluginName ] = function ( options ) {
  144. var e = this;
  145. e.each(function() {
  146. if ( !$.data( e, "plugin_" + pluginName ) ) {
  147. $.data( e, "plugin_" + pluginName, new Plugin( this, options ) );
  148. }
  149. });
  150. // chain jQuery functions
  151. return e;
  152. };
  153. })( jQuery, window, document );