export_excel.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const SpreadExcelObj = (function() {
  10. const _createHideSpread = function () {
  11. const div = document.createElement("div");
  12. div.setAttribute('id', 'exportExcelSpread');
  13. // $(div).css("position", "absolute").css('top', 0).css('left', 0).css('height', 300).css('width', 500).css('z-index', 999);
  14. // document.body.insertBefore(div, null);
  15. div.style.display = 'none';
  16. return div;
  17. };
  18. const _removeHideSpread = function (div) {
  19. document.body.removeChild(div);
  20. };
  21. const exportSpread2XlsxWithHeader = function (spread, file) {
  22. spread.getActiveSheet().options.isProtected = false;
  23. const excelIo = new GC.Spread.Excel.IO();
  24. const sJson = JSON.stringify(spread.toJSON({columnHeadersAsFrozenRows: true, rowHeadersAsFrozenColumns: true}));
  25. excelIo.save(sJson, function(blob) {
  26. saveAs(blob, file);
  27. });
  28. spread.getActiveSheet().options.isProtected = true;
  29. };
  30. const exportSimpleXlsxSheet = function (setting, data, file) {
  31. const div = _createHideSpread();
  32. const spread = SpreadJsObj.createNewSpread(div);
  33. const sheet = spread.getActiveSheet();
  34. SpreadJsObj.beginMassOperation(sheet);
  35. sheet.options.isProtected = false;
  36. sheet.setColumnCount(setting.cols.length);
  37. sheet.setRowCount(setting.headRows + data.length);
  38. for (let iRow = 0; iRow < setting.headRowHeight.length; iRow++) {
  39. sheet.setRowHeight(iRow, setting.headRowHeight[iRow]);
  40. }
  41. for (let iCol = 0; iCol < setting.cols.length; iCol++) {
  42. const col = setting.cols[iCol];
  43. const title = col.title.split('|');
  44. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  45. for (let i = 0; i < title.length; i++) {
  46. const cell = sheet.getCell(i, iCol);
  47. cell.text(title[i]).wordWrap(true).hAlign(1).vAlign(1).font(setting.headerFont);
  48. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  49. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]));
  50. }
  51. }
  52. sheet.setColumnWidth(iCol, col.width);
  53. if (col.visible !== undefined && col.visible !== null) {
  54. sheet.setColumnVisible(iCol, col.visible);
  55. }
  56. }
  57. for (let iRow = 0; iRow < data.length; iRow++) {
  58. const curRow = setting.headRows + iRow;
  59. const d = data[iRow];
  60. for (let iCol = 0; iCol < setting.cols.length; iCol++) {
  61. const cell = sheet.getCell(curRow, iCol);
  62. const col = setting.cols[iCol];
  63. if (col.field !== '' && d[col.field]) {
  64. cell.value(d[col.field]);
  65. if (typeof d[col.field] === 'string') {
  66. cell.formatter('@');
  67. }
  68. }
  69. if (col.font) {
  70. cell.font(col.font);
  71. } else if (setting.font) {
  72. cell.font(setting.font);
  73. }
  74. cell.hAlign(col.hAlign);
  75. }
  76. }
  77. SpreadJsObj.endMassOperation(sheet);
  78. const excelIo = new GC.Spread.Excel.IO();
  79. const sJson = JSON.stringify(spread.toJSON());
  80. excelIo.save(sJson, function(blob) {
  81. saveAs(blob, file);
  82. _removeHideSpread(div);
  83. });
  84. };
  85. return {exportSimpleXlsxSheet, exportSpread2XlsxWithHeader}
  86. })();
  87. const XLSXObj = (function () {
  88. const exportXlsxSheet = function (setting, data, file) {
  89. const headerStyle = {
  90. font: { sz: 10, bold: true },
  91. alignment: {horizontal: 'center'},
  92. };
  93. const sHeader = setting.header
  94. .map((v, i) => Object.assign({}, {v: v, s: headerStyle, position: String.fromCharCode(65+i) + 1 }))
  95. .reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v, s: next.s}}), {});
  96. const sData = data
  97. .map((v, i) => v.map((k, j) => Object.assign({}, {
  98. v: k ? k : '',
  99. s: { font: { sz: 10 }, alignment: {horizontal: setting.hAlign[j]}},
  100. position: String.fromCharCode(65+j) + (i+2) })))
  101. .reduce((prev, next) => prev.concat(next))
  102. .reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v, s: next.s}}), {});
  103. const output = Object.assign({}, sHeader, sData);
  104. const outputPos = Object.keys(output);
  105. const result = Object.assign({}, output,
  106. {'!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1]},
  107. {'!cols': setting.width.map((w) => Object.assign({}, {wpx: w}))});
  108. const xlsxData = {
  109. SheetNames: ['Sheet1'],
  110. Sheets: {
  111. 'Sheet1': result
  112. }
  113. };
  114. const blob = xlsxUtils.format2Blob(xlsxData);
  115. saveAs(blob, file);
  116. };
  117. return {exportXlsxSheet}
  118. });