excel.js 4.4 KB

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