excel.js 3.4 KB

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