excel.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var Excel =
  2. {
  3. open : function(data, filename, sheetname) {
  4. this.name = sheetname || 'sheet1';
  5. this.sheet = XLSX.utils.aoa_to_sheet(data, {raw: true});
  6. this.autoWidthFunc(this.sheet, data);
  7. if (filename.substr(-5).toLowerCase() !== '.xlsx') {
  8. filename += '.xlsx';
  9. }
  10. this.openDownloadDialog(this.sheet2blob(this.sheet, this.name), filename);
  11. },
  12. autoWidthFunc : function (ws, data) {
  13. // set worksheet max width per col
  14. const colWidth = data.map(row =>
  15. row.map(val => {
  16. // if null/undefined
  17. if (val == null) {
  18. return { wch: 10 };
  19. } else if (val.toString().charCodeAt(0) > 255) {
  20. // if chinese
  21. var l = val.toString().length;
  22. var n = l + l*0.5;
  23. return { wch: n * 2 };
  24. } else {
  25. var l = val.toString().length;
  26. var n = l + l*0.5;
  27. return { wch: n };
  28. }
  29. })
  30. );
  31. // start in the first row
  32. const result = colWidth[0];
  33. for (let i = 1; i < colWidth.length; i++) {
  34. for (let j = 0; j < colWidth[i].length; j++) {
  35. if (result[j].wch < colWidth[i][j].wch) {
  36. result[j].wch = colWidth[i][j].wch;
  37. }
  38. }
  39. }
  40. ws['!cols'] = result;
  41. },
  42. export : function (url, data) {
  43. var self = this;
  44. var filename = '';
  45. var head = [];
  46. var alldata = [];
  47. var loading = layer.msg('正在加载 <span data-upload-page></span>,完成<span data-upload-progress>0</span>%', {time:0,icon: 1});
  48. nextPage(1, 1);
  49. function nextPage(curPage, maxPage) {
  50. if (curPage > maxPage) {
  51. alldata.unshift(head);
  52. this.result = alldata;
  53. if (this.result !== false) {
  54. self.open(this.result, filename || '文件下载.xlsx');
  55. } else {
  56. console.log('格式化函数返回`false`,已终止数据导出操作', alldata, this.result);
  57. }
  58. layer.close(loading);
  59. } else {
  60. $.post(url, {excel_type:1,pg:curPage,json:1}, function (ret) {
  61. var ret = eval('(' + ret + ')');
  62. if (ret.status == 1 && ret.data) {
  63. filename = ret.data.filename;
  64. head = ret.data.head;
  65. alldata = alldata.concat(ret.data.body);
  66. curPage = ret.page.current_page;
  67. maxPage = ret.page.total_page;
  68. $('[data-upload-page]').html(curPage + '/' + maxPage);
  69. $('[data-upload-progress]').html((curPage / maxPage * 100).toFixed(2));
  70. return nextPage(curPage + 1, maxPage, false);
  71. } else {
  72. layer.close(loading);
  73. layer.alert(ret.msg);
  74. }
  75. }, false);
  76. }
  77. }
  78. },
  79. /*! Sheet 转下载对象 */
  80. sheet2blob : function(sheet, name) {
  81. this.workbook = {SheetNames: [name], Sheets: {}};
  82. this.workbook.Sheets[name] = sheet;
  83. this.content = XLSX.write(this.workbook, {
  84. type: 'binary', bookSST: false, bookType: 'xlsx',
  85. });
  86. return new Blob([this.toArrayBuffer(this.content)], {
  87. type: "application/octet-stream"
  88. });
  89. },
  90. /*! 字符串转 ArrayBuffer */
  91. toArrayBuffer : function(s) {
  92. this.buff = new ArrayBuffer(s.length);
  93. this.view = new Uint8Array(this.buff);
  94. for (this.index = 0; this.index !== s.length; ++this.index) {
  95. this.view[this.index] = s.charCodeAt(this.index) & 0xFF;
  96. }
  97. return this.buff;
  98. },
  99. /*! 通用的打开下载对话框方法 */
  100. openDownloadDialog : function(location, filename) {
  101. if (typeof location == 'object' && location instanceof Blob) {
  102. location = URL.createObjectURL(location);
  103. }
  104. this.link = document.createElement('a');
  105. this.link.download = filename || Date.now() + '.xlsx';
  106. this.link.href = location;
  107. if (window.MouseEvent) {
  108. this.event = new MouseEvent('click');
  109. } else {
  110. this.event = document.createEvent('MouseEvents');
  111. this.event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  112. }
  113. this.link.dispatchEvent(this.event);
  114. }
  115. }