export_excel.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 exportSimpleXlsxSheetData = function (sheet, setting, data) {
  22. SpreadJsObj.beginMassOperation(sheet);
  23. sheet.options.isProtected = false;
  24. sheet.setColumnCount(setting.cols.length);
  25. sheet.setRowCount(setting.headRows + data.length);
  26. for (let iRow = 0; iRow < setting.headRowHeight.length; iRow++) {
  27. sheet.setRowHeight(iRow, setting.headRowHeight[iRow]);
  28. }
  29. for (let iCol = 0; iCol < setting.cols.length; iCol++) {
  30. const col = setting.cols[iCol];
  31. const title = col.title.split('|');
  32. const colSpan = col.colSpan ? col.colSpan.split('|') : ['1'],
  33. rowSpan = col.rowSpan ? col.rowSpan.split('|') : ['1'];
  34. for (let i = 0; i < title.length; i++) {
  35. const cell = sheet.getCell(i, iCol);
  36. cell.text(title[i]).wordWrap(true).hAlign(1).vAlign(1).font(setting.headerFont);
  37. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  38. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]));
  39. }
  40. }
  41. sheet.setColumnWidth(iCol, col.width);
  42. if (col.visible !== undefined && col.visible !== null) {
  43. sheet.setColumnVisible(iCol, col.visible);
  44. }
  45. }
  46. for (let iRow = 0; iRow < data.length; iRow++) {
  47. const curRow = setting.headRows + iRow;
  48. const d = data[iRow];
  49. for (let iCol = 0; iCol < setting.cols.length; iCol++) {
  50. const cell = sheet.getCell(curRow, iCol);
  51. const col = setting.cols[iCol];
  52. if (col.field !== '' && d[col.field]) {
  53. cell.value(col.getValue ? col.getValue(d) : d[col.field]);
  54. if (typeof d[col.field] === 'string') {
  55. cell.formatter('@');
  56. }
  57. }
  58. if (col.font) {
  59. cell.font(col.font);
  60. } else if (setting.font) {
  61. cell.font(setting.font);
  62. }
  63. cell.hAlign(col.hAlign);
  64. }
  65. }
  66. SpreadJsObj.endMassOperation(sheet);
  67. };
  68. const exportSimpleXlsxSheet = function (setting, data, file) {
  69. const div = _createHideSpread();
  70. const spread = SpreadJsObj.createNewSpread(div, true);
  71. const sheet = spread.getActiveSheet();
  72. exportSimpleXlsxSheetData(sheet, setting, data);
  73. const excelIo = new GC.Spread.Excel.IO();
  74. const sJson = JSON.stringify(spread.toJSON());
  75. excelIo.save(sJson, function(blob) {
  76. saveAs(blob, file);
  77. _removeHideSpread(div);
  78. });
  79. };
  80. const exportSimpleXlsxSheets = function (sheets, file) {
  81. if (!sheets || sheets.length === 0) return;
  82. const div = _createHideSpread();
  83. const spread = new spreadNS.Workbook(div, {sheetCount: sheets.length});
  84. for (const [i, sheetData] of sheets.entries()) {
  85. const sheet = spread.getSheet(i);
  86. sheet.name(sheetData.name);
  87. exportSimpleXlsxSheetData(sheet, sheetData.setting, sheetData.data);
  88. }
  89. const excelIo = new GC.Spread.Excel.IO();
  90. const sJson = JSON.stringify(spread.toJSON());
  91. excelIo.save(sJson, function(blob) {
  92. saveAs(blob, file);
  93. _removeHideSpread(div);
  94. });
  95. };
  96. const exportSpread2XlsxWithHeader = function (spread, file) {
  97. spread.getActiveSheet().options.isProtected = false;
  98. const excelIo = new GC.Spread.Excel.IO();
  99. const sJson = JSON.stringify(spread.toJSON({columnHeadersAsFrozenRows: true, rowHeadersAsFrozenColumns: true}));
  100. excelIo.save(sJson, function(blob) {
  101. saveAs(blob, file);
  102. });
  103. spread.getActiveSheet().options.isProtected = true;
  104. };
  105. return {exportSimpleXlsxSheet, exportSpread2XlsxWithHeader, exportSimpleXlsxSheets}
  106. })();
  107. const XLSXObj = (function () {
  108. const transportSheetData = function (setting, data) {
  109. const headerStyle = {
  110. font: { sz: 10, bold: true },
  111. alignment: { horizontal: 'center' },
  112. };
  113. const sHeader = setting.header
  114. .map((v, i) => Object.assign({}, {v: v, s: headerStyle, position: String.fromCharCode(65+i) + 1 }))
  115. .reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v, s: next.s}}), {});
  116. const sData = data
  117. .map((v, i) => v.map((k, j) => Object.assign({}, {
  118. v: k ? k : '',
  119. s: { font: { sz: 10 }, alignment: {horizontal: setting.hAlign[j]}},
  120. position: String.fromCharCode(65+j) + (i+2) })))
  121. .reduce((prev, next) => prev.concat(next))
  122. .reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v, s: next.s}}), {});
  123. const output = Object.assign({}, sHeader, sData);
  124. const outputPos = Object.keys(output);
  125. const result = Object.assign({}, output,
  126. {'!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1]},
  127. {'!cols': setting.width.map((w) => Object.assign({}, {wpx: w}))});
  128. return result;
  129. };
  130. const exportXlsxSheet = function (setting, data, file) {
  131. const result = transportSheetData(setting, data);
  132. const xlsxData = {
  133. SheetNames: ['Sheet1'],
  134. Sheets: {
  135. 'Sheet1': result
  136. }
  137. };
  138. const blob = xlsxUtils.format2Blob(xlsxData);
  139. saveAs(blob, file);
  140. };
  141. const exportXlsxSheets = function (sheets, file) {
  142. const xlsxData = { SheetNames: [], Sheets: {} };
  143. for (const sheet of sheets) {
  144. const xlsxSheet = transportSheetData(sheet.setting, sheet.data);
  145. xlsxData.SheetNames.push(sheet.name);
  146. xlsxData.Sheets[sheet.name] = xlsxSheet;
  147. }
  148. const blob = xlsxUtils.format2Blob(xlsxData);
  149. saveAs(blob, file);
  150. };
  151. return { exportXlsxSheet, exportXlsxSheets }
  152. })();