d12d37ccf4786cde7ec008a84d6f86d19d499f4b.svn-base 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* eslint-disable */
  2. import { saveAs } from 'file-saver'
  3. import XLSX from 'xlsx'
  4. function generateArray(table) {
  5. var out = [];
  6. var rows = table.querySelectorAll('tr');
  7. var ranges = [];
  8. for (var R = 0; R < rows.length; ++R) {
  9. var outRow = [];
  10. var row = rows[R];
  11. var columns = row.querySelectorAll('td');
  12. for (var C = 0; C < columns.length; ++C) {
  13. var cell = columns[C];
  14. var colspan = cell.getAttribute('colspan');
  15. var rowspan = cell.getAttribute('rowspan');
  16. var cellValue = cell.innerText;
  17. if (cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue;
  18. //Skip ranges
  19. ranges.forEach(function (range) {
  20. if (R >= range.s.r && R <= range.e.r && outRow.length >= range.s.c && outRow.length <= range.e.c) {
  21. for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null);
  22. }
  23. });
  24. //Handle Row Span
  25. if (rowspan || colspan) {
  26. rowspan = rowspan || 1;
  27. colspan = colspan || 1;
  28. ranges.push({
  29. s: {
  30. r: R,
  31. c: outRow.length
  32. },
  33. e: {
  34. r: R + rowspan - 1,
  35. c: outRow.length + colspan - 1
  36. }
  37. });
  38. };
  39. //Handle Value
  40. outRow.push(cellValue !== "" ? cellValue : null);
  41. //Handle Colspan
  42. if (colspan)
  43. 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. console.log(data,'data')
  67. for (var R = 0; R != data.length; ++R) {
  68. if(data[R] !== undefined){
  69. for (var C = 0; C != data[R].length; ++C) {
  70. if (range.s.r > R) range.s.r = R;
  71. if (range.s.c > C) range.s.c = C;
  72. if (range.e.r < R) range.e.r = R;
  73. if (range.e.c < C) range.e.c = C;
  74. var cell = {
  75. v: data[R][C]
  76. };
  77. if (cell.v == null) continue;
  78. var cell_ref = XLSX.utils.encode_cell({
  79. c: C,
  80. r: R
  81. });
  82. if (typeof cell.v === 'number') cell.t = 'n';
  83. else if (typeof cell.v === 'boolean') cell.t = 'b';
  84. else if (cell.v instanceof Date) {
  85. cell.t = 'n';
  86. cell.z = XLSX.SSF._table[14];
  87. cell.v = datenum(cell.v);
  88. }
  89. else cell.t = 's';
  90. ws[cell_ref] = cell;
  91. }
  92. }
  93. }
  94. if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
  95. return ws;
  96. }
  97. function Workbook() {
  98. if (!(this instanceof Workbook)) return new Workbook();
  99. this.SheetNames = [];
  100. this.Sheets = {};
  101. }
  102. function s2ab(s) {
  103. var buf = new ArrayBuffer(s.length);
  104. var view = new Uint8Array(buf);
  105. for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  106. return buf;
  107. }
  108. export function export_table_to_excel(id) {
  109. var theTable = document.getElementById(id);
  110. var oo = generateArray(theTable);
  111. var ranges = oo[1];
  112. /* original data */
  113. var data = oo[0];
  114. var ws_name = "SheetJS";
  115. var wb = new Workbook(),
  116. ws = sheet_from_array_of_arrays(data);
  117. /* add ranges to worksheet */
  118. // ws['!cols'] = ['apple', 'banan'];
  119. ws['!merges'] = ranges;
  120. /* add worksheet to workbook */
  121. wb.SheetNames.push(ws_name);
  122. wb.Sheets[ws_name] = ws;
  123. var wbout = XLSX.write(wb, {
  124. bookType: 'xlsx',
  125. bookSST: false,
  126. type: 'binary'
  127. });
  128. saveAs(new Blob([s2ab(wbout)], {
  129. type: "application/octet-stream"
  130. }), "test.xlsx")
  131. }
  132. // 对此方法进行修改,如下:
  133. export function export_json_to_excel({
  134. multiHeader2 = [], // 第一行表头
  135. multiHeader = [], // 第二行表头
  136. header, // 第三行表头
  137. data,
  138. filename, //文件名
  139. merges = [], // 合并
  140. autoWidth = true,
  141. bookType = 'xlsx'
  142. } = {}) {
  143. /* original data */
  144. filename = filename || '列表';
  145. data = [...data]
  146. data.unshift(header);
  147. for (let i = multiHeader2.length - 1; i > -1; i--) {
  148. data.unshift(multiHeader2[i])
  149. }
  150. for (let i = multiHeader.length - 1; i > -1; i--) {
  151. data.unshift(multiHeader[i])
  152. }
  153. var ws_name = "SheetJS";
  154. var wb = new Workbook(),
  155. ws = sheet_from_array_of_arrays(data);
  156. if (merges.length > 0) {
  157. if (!ws['!merges']) ws['!merges'] = [];
  158. merges.forEach(item => {
  159. ws['!merges'].push(XLSX.utils.decode_range(item))
  160. })
  161. }
  162. if (autoWidth) {
  163. /*设置worksheet每列的最大宽度*/
  164. console.log('data',data)
  165. for(let i=0;i<data.length;i++){
  166. if(data[i] == undefined){
  167. data.splice(i,1)
  168. }
  169. }
  170. const colWidth = data.map(row => row.map(val => {
  171. /*先判断是否为null/undefined*/
  172. if (val == null) {
  173. return {
  174. 'wch': 10
  175. };
  176. }
  177. /*再判断是否为中文*/
  178. else if (val.toString().charCodeAt(0) > 255) {
  179. return {
  180. 'wch': val.toString().length * 2
  181. };
  182. } else {
  183. return {
  184. 'wch': val.toString().length
  185. };
  186. }
  187. }))
  188. /*以第一行为初始值*/
  189. let result = colWidth[0];
  190. for (let i = 1; i < colWidth.length; i++) {
  191. for (let j = 0; j < colWidth[i].length; j++) {
  192. if (result[j]['wch'] < colWidth[i][j]['wch']) {
  193. result[j]['wch'] = colWidth[i][j]['wch'];
  194. }
  195. }
  196. }
  197. ws['!cols'] = result;
  198. }
  199. /* add worksheet to workbook */
  200. wb.SheetNames.push(ws_name);
  201. wb.Sheets[ws_name] = ws;
  202. var wbout = XLSX.write(wb, {
  203. bookType: bookType,
  204. bookSST: false,
  205. type: 'binary'
  206. });
  207. saveAs(new Blob([s2ab(wbout)], {
  208. type: "application/octet-stream"
  209. }), `${filename}.${bookType}`);
  210. }