excel.xlsx.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. define(['xlsx'], function () {
  2. function excel(data, filename, sheetname) {
  3. this.name = sheetname || 'sheet1';
  4. this.sheet = XLSX.utils.aoa_to_sheet(data);
  5. if (filename.substr(-5).toLowerCase() !== '.xlsx') {
  6. filename += '.xlsx';
  7. }
  8. openDownloadDialog(sheet2blob(this.sheet, this.name), filename);
  9. }
  10. excel.bind = function (done, filename) {
  11. $('body').off('click', '[data-form-export]').on('click', '[data-form-export]', function () {
  12. var form = $(this).parents('form');
  13. var method = $(this).attr('data-method') || 'get';
  14. var location = $(this).attr('data-form-export') || form.get(0).action || '';
  15. excel.load(location, form.serialize(), done, filename, method);
  16. })
  17. };
  18. excel.load = function (url, data, done, filename, method) {
  19. var alldata = [];
  20. var loading = $.msg.loading('正在加载 <span data-upload-page></span>,完成<span data-upload-progress>0</span>%');
  21. nextPage(1, 1);
  22. function nextPage(curPage, maxPage) {
  23. if (curPage > maxPage) {
  24. if (typeof done === 'function') {
  25. this.result = done(alldata);
  26. if (this.result !== false) {
  27. excel(this.result, filename || '文件下载.xlsx');
  28. } else {
  29. console.log('格式化函数返回`false`,已终止数据导出操作', alldata, this.result);
  30. }
  31. } else {
  32. console.log('格式化函数未绑定,已终止数据导出操作', alldata);
  33. }
  34. $.msg.close(loading);
  35. } else {
  36. $('[data-upload-page]').html(curPage + '/' + maxPage);
  37. $('[data-upload-progress]').html((curPage / maxPage * 100).toFixed(2));
  38. $.form.load(url + (url.indexOf('?') > -1 ? '&' : '?') + 'output=json&page=' + curPage, data, method || 'get', function (ret) {
  39. if (ret.code) {
  40. alldata = alldata.concat(ret.data.list);
  41. return nextPage((ret.data.page.current || 1) + 1, ret.data.page.pages || 1), false;
  42. }
  43. }, false);
  44. }
  45. }
  46. }
  47. return excel;
  48. /*! Sheet 转下载对象 */
  49. function sheet2blob(sheet, name) {
  50. this.workbook = {SheetNames: [name], Sheets: {}};
  51. this.workbook.Sheets[name] = sheet;
  52. this.content = XLSX.write(this.workbook, {
  53. type: 'binary', bookSST: false, bookType: 'xlsx',
  54. });
  55. return new Blob([toArrayBuffer(this.content)], {
  56. type: "application/octet-stream"
  57. });
  58. }
  59. /*! 字符串转 ArrayBuffer */
  60. function toArrayBuffer(s) {
  61. this.buff = new ArrayBuffer(s.length);
  62. this.view = new Uint8Array(this.buff);
  63. for (this.index = 0; this.index !== s.length; ++this.index) {
  64. this.view[this.index] = s.charCodeAt(this.index) & 0xFF;
  65. }
  66. return this.buff;
  67. }
  68. /*! 通用的打开下载对话框方法 */
  69. function openDownloadDialog(location, filename) {
  70. if (typeof location == 'object' && location instanceof Blob) {
  71. location = URL.createObjectURL(location);
  72. }
  73. this.link = document.createElement('a');
  74. this.link.download = filename || Date.now() + '.xlsx';
  75. this.link.href = location;
  76. if (window.MouseEvent) {
  77. this.event = new MouseEvent('click');
  78. } else {
  79. this.event = document.createEvent('MouseEvents');
  80. this.event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  81. }
  82. this.link.dispatchEvent(this.event);
  83. }
  84. });