cf6123c4a50aeaa889dde9e918dd5c2ec760f05e.svn-base 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* eslint-disable */
  2. import { saveAs } from 'file-saver'
  3. // import saveAs from 'vue-json-excel'
  4. import XLSX from 'xlsx'
  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)
  44. for (var k = 0; k < colspan - 1; ++k) outRow.push(null);
  45. }
  46. out.push(outRow);
  47. }
  48. return [out, ranges];
  49. };
  50. function datenum(v, date1904) {
  51. if (date1904) v += 1462;
  52. var epoch = Date.parse(v);
  53. return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
  54. }
  55. function sheet_from_array_of_arrays(data, opts) {
  56. var ws = {};
  57. var range = {
  58. s: {
  59. c: 10000000,
  60. r: 10000000
  61. },
  62. e: {
  63. c: 0,
  64. r: 0
  65. }
  66. };
  67. for (var R = 0; R != data.length; ++R) {
  68. for (var C = 0; C != data[R].length; ++C) {
  69. if (range.s.r > R) range.s.r = R;
  70. if (range.s.c > C) range.s.c = C;
  71. if (range.e.r < R) range.e.r = R;
  72. if (range.e.c < C) range.e.c = C;
  73. var cell = {
  74. v: data[R][C]
  75. };
  76. if (cell.v == null) continue;
  77. var cell_ref = XLSX.utils.encode_cell({
  78. c: C,
  79. r: R
  80. });
  81. if (typeof cell.v === 'number') cell.t = 'n';
  82. else if (typeof cell.v === 'boolean') cell.t = 'b';
  83. else if (cell.v instanceof Date) {
  84. cell.t = 'n';
  85. cell.z = XLSX.SSF._table[14];
  86. cell.v = datenum(cell.v);
  87. } else cell.t = 's';
  88. ws[cell_ref] = cell;
  89. }
  90. }
  91. if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
  92. return ws;
  93. }
  94. function Workbook() {
  95. if (!(this instanceof Workbook)) return new Workbook();
  96. this.SheetNames = [];
  97. this.Sheets = {};
  98. }
  99. function s2ab(s) {
  100. var buf = new ArrayBuffer(s.length);
  101. var view = new Uint8Array(buf);
  102. for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  103. return buf;
  104. }
  105. export function export_table_to_excel(id) {
  106. var theTable = document.getElementById(id);
  107. var oo = generateArray(theTable);
  108. var ranges = oo[1];
  109. /* original data */
  110. var data = oo[0];
  111. var ws_name = "sheet";
  112. var wb = new Workbook(),
  113. ws = sheet_from_array_of_arrays(data);
  114. /* add ranges to worksheet */
  115. // ws['!cols'] = ['apple', 'banan'];
  116. ws['!merges'] = ranges;
  117. /* add worksheet to workbook */
  118. wb.SheetNames.push(ws_name);
  119. wb.Sheets[ws_name] = ws;
  120. var wbout = XLSX.write(wb, {
  121. bookType: 'xlsx',
  122. bookSST: false,
  123. type: 'binary'
  124. });
  125. saveAs(new Blob([s2ab(wbout)], {
  126. type: "application/octet-stream"
  127. }), "test.xlsx")
  128. }
  129. export function export_json_to_excel({
  130. multiHeader = [],
  131. multiHeader2 = [],
  132. // header,
  133. data,
  134. filename,
  135. merges = [],
  136. autoWidth = true,
  137. bookType = 'xlsx'
  138. } = {}) {
  139. /* original data */
  140. filename = filename || 'excel-list'
  141. data = [...data]
  142. // data.unshift(header);
  143. for (let i = multiHeader2.length - 1; i > -1; i--) {
  144. data.unshift(multiHeader2[i])
  145. }
  146. for (let i = multiHeader.length - 1; i > -1; i--) {
  147. data.unshift(multiHeader[i])
  148. }
  149. var ws_name = "sheet";
  150. var wb = new Workbook(),
  151. ws = sheet_from_array_of_arrays(data);
  152. if (merges.length > 0) {
  153. if (!ws['!merges']) ws['!merges'] = [];
  154. merges.forEach(item => {
  155. ws['!merges'].push(XLSX.utils.decode_range(item))
  156. })
  157. }
  158. if (autoWidth) {
  159. /*设置worksheet每列的最大宽度*/
  160. const colWidth = data.map(row => row.map(val => {
  161. /*先判断是否为null/undefined*/
  162. if (val == null) {
  163. return {
  164. 'wch': 10
  165. };
  166. }
  167. /*再判断是否为中文*/
  168. else if (val.toString().charCodeAt(0) > 255) {
  169. return {
  170. 'wch': val.toString().length * 2
  171. };
  172. } else {
  173. return {
  174. 'wch': val.toString().length
  175. };
  176. }
  177. }))
  178. /*以第一行为初始值*/
  179. let result = colWidth[0];
  180. for (let i = 1; i < colWidth.length; i++) {
  181. for (let j = 0; j < colWidth[i].length; j++) {
  182. if (result[j]['wch'] < colWidth[i][j]['wch']) {
  183. result[j]['wch'] = colWidth[i][j]['wch'];
  184. }
  185. }
  186. }
  187. ws['!cols'] = result;
  188. }
  189. /* add worksheet to workbook */
  190. wb.SheetNames.push(ws_name);
  191. wb.Sheets[ws_name] = ws;
  192. var wbout = XLSX.write(wb, {
  193. bookType: bookType,
  194. bookSST: false,
  195. type: 'binary'
  196. });
  197. saveAs(new Blob([s2ab(wbout)], {
  198. type: "application/octet-stream"
  199. }), `${filename}.${bookType}`);
  200. }