table2excel.js 5.5 KB

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