export_excel.js 4.8 KB

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