export_excel.js 4.7 KB

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