export_excel.js 5.1 KB

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