export2Excel.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* eslint-disable */
  2. require('script-loader!file-saver')
  3. import XLSX from 'xlsx'
  4. // import FileSaver from 'file-saver'
  5. function generateArray(table) {
  6. var out = []
  7. var rows = table.querySelectorAll('tr')
  8. var ranges = []
  9. for (var R = 0; R < rows.length; ++R) {
  10. var outRow = []
  11. var row = rows[R]
  12. var columns = row.querySelectorAll('td')
  13. for (var C = 0; C < columns.length; ++C) {
  14. var cell = columns[C]
  15. var colspan = cell.getAttribute('colspan')
  16. var rowspan = cell.getAttribute('rowspan')
  17. var cellValue = cell.innerText
  18. if (cellValue !== '' && cellValue === +cellValue) cellValue = +cellValue
  19. // Skip ranges
  20. ranges.forEach(function(range) {
  21. if (R >= range.s.r && R <= range.e.r && outRow.length >= range.s.c && outRow.length <= range.e.c) {
  22. for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null)
  23. }
  24. })
  25. // Handle Row Span
  26. if (rowspan || colspan) {
  27. rowspan = rowspan || 1
  28. colspan = colspan || 1
  29. ranges.push({
  30. s: {
  31. r: R,
  32. c: outRow.length
  33. },
  34. e: {
  35. r: R + rowspan - 1,
  36. c: outRow.length + colspan - 1
  37. }
  38. })
  39. }
  40. // Handle Value
  41. outRow.push(cellValue !== '' ? cellValue : null)
  42. // Handle Colspan
  43. if (colspan) { for (var k = 0; k < colspan - 1; ++k) outRow.push(null) }
  44. }
  45. out.push(outRow)
  46. }
  47. return [out, ranges]
  48. }
  49. function datenum(v, date1904) {
  50. if (date1904) v += 1462
  51. var epoch = Date.parse(v)
  52. return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000)
  53. }
  54. function sheet_from_array_of_arrays(data, opts) {
  55. var ws = {}
  56. var range = {
  57. s: {
  58. c: 10000000,
  59. r: 10000000
  60. },
  61. e: {
  62. c: 0,
  63. r: 0
  64. }
  65. }
  66. for (var R = 0; R !== data.length; ++R) {
  67. for (var C = 0; C !== data[R].length; ++C) {
  68. if (range.s.r > R) range.s.r = R
  69. if (range.s.c > C) range.s.c = C
  70. if (range.e.r < R) range.e.r = R
  71. if (range.e.c < C) range.e.c = C
  72. var cell = {
  73. v: data[R][C]
  74. }
  75. if (cell.v == null) continue
  76. var cell_ref = XLSX.utils.encode_cell({
  77. c: C,
  78. r: R
  79. })
  80. if (typeof cell.v === 'number') cell.t = 'n'
  81. else if (typeof cell.v === 'boolean') cell.t = 'b'
  82. else if (cell.v instanceof Date) {
  83. cell.t = 'n'
  84. cell.z = XLSX.SSF._table[14]
  85. cell.v = datenum(cell.v)
  86. } else cell.t = 's'
  87. ws[cell_ref] = cell
  88. }
  89. }
  90. if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range)
  91. return ws
  92. }
  93. function Workbook() {
  94. if (!(this instanceof Workbook)) return new Workbook()
  95. this.SheetNames = []
  96. this.Sheets = {}
  97. }
  98. function s2ab(s) {
  99. var buf = new ArrayBuffer(s.length)
  100. var view = new Uint8Array(buf)
  101. for (var i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF
  102. return buf
  103. }
  104. export function export_table_to_excel(id) {
  105. var theTable = document.getElementById(id)
  106. var oo = generateArray(theTable)
  107. var ranges = oo[1]
  108. /* original data */
  109. var data = oo[0]
  110. var ws_name = 'SheetJS'
  111. var wb = new Workbook()
  112. var ws = sheet_from_array_of_arrays(data)
  113. /* add ranges to worksheet */
  114. // ws['!cols'] = ['apple', 'banan'];
  115. ws['!merges'] = ranges
  116. /* add worksheet to workbook */
  117. wb.SheetNames.push(ws_name)
  118. wb.Sheets[ws_name] = ws
  119. var wbout = XLSX.write(wb, {
  120. bookType: 'xlsx',
  121. bookSST: false,
  122. type: 'binary'
  123. })
  124. saveAs(new Blob([s2ab(wbout)], {
  125. type: 'application/octet-stream'
  126. }), 'test.xlsx')
  127. }
  128. export function export_json_to_excel({
  129. header,
  130. data,
  131. filename,
  132. autoWidth = true,
  133. bookType = 'xlsx'
  134. } = {}) {
  135. /* original data */
  136. filename = filename || 'excel-list'
  137. data = [...data]
  138. data.unshift(header)
  139. var ws_name = 'SheetJS'
  140. var wb = new Workbook()
  141. var ws = sheet_from_array_of_arrays(data)
  142. if (autoWidth) {
  143. /* 设置worksheet每列的最大宽度*/
  144. const colWidth = data.map(row => row.map(val => {
  145. /* 先判断是否为null/undefined*/
  146. if (val == null) {
  147. return {
  148. 'wch': 10
  149. }
  150. } else if (val.toString().charCodeAt(0) > 255) {
  151. /* 再判断是否为中文*/
  152. return {
  153. 'wch': val.toString().length * 2
  154. }
  155. } else {
  156. return {
  157. 'wch': val.toString().length
  158. }
  159. }
  160. }))
  161. /* 以第一行为初始值*/
  162. const result = colWidth[0]
  163. for (let i = 1; i < colWidth.length; i++) {
  164. for (let j = 0; j < colWidth[i].length; j++) {
  165. if (result[j]['wch'] < colWidth[i][j]['wch']) {
  166. result[j]['wch'] = colWidth[i][j]['wch']
  167. }
  168. }
  169. }
  170. ws['!cols'] = result
  171. }
  172. /* add worksheet to workbook */
  173. wb.SheetNames.push(ws_name)
  174. wb.Sheets[ws_name] = ws
  175. var wbout = XLSX.write(wb, {
  176. bookType: bookType,
  177. bookSST: false,
  178. type: 'binary'
  179. })
  180. saveAs(new Blob([s2ab(wbout)], {
  181. type: 'application/octet-stream'
  182. }), `${filename}.${bookType}`)
  183. }