export_excel.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. const autoFit = !!setting.cols.find(x => { return x.wordWrap; });
  47. for (let iRow = 0; iRow < data.length; iRow++) {
  48. const curRow = setting.headRows + iRow;
  49. const d = data[iRow];
  50. for (let iCol = 0; iCol < setting.cols.length; iCol++) {
  51. const cell = sheet.getCell(curRow, iCol);
  52. const col = setting.cols[iCol];
  53. if (col.field !== '' && d[col.field]) {
  54. cell.value(col.getValue ? col.getValue(d) : d[col.field]);
  55. if (col.wordWrap) cell.wordWrap(true);
  56. if (typeof d[col.field] === 'string') {
  57. cell.formatter('@');
  58. }
  59. }
  60. if (col.font) {
  61. cell.font(col.font);
  62. } else if (setting.font) {
  63. cell.font(setting.font);
  64. }
  65. cell.hAlign(col.hAlign).vAlign(1);
  66. }
  67. if (autoFit) sheet.autoFitRow(curRow);
  68. }
  69. SpreadJsObj.endMassOperation(sheet);
  70. };
  71. const exportXlsxSheetGroup = function(sheet, setting, group) {
  72. if (!group) return;
  73. for (const g of group) {
  74. sheet.rowOutlines.group(g.start + (setting.headRows || 0), g.count);
  75. }
  76. };
  77. const exportSimpleXlsxSheet = function (setting, data, file, group) {
  78. const div = _createHideSpread();
  79. const spread = SpreadJsObj.createNewSpread(div, true);
  80. const sheet = spread.getActiveSheet();
  81. exportSimpleXlsxSheetData(sheet, setting, data);
  82. exportXlsxSheetGroup(sheet, setting, group);
  83. const excelIo = new GC.Spread.Excel.IO();
  84. const sJson = JSON.stringify(spread.toJSON());
  85. excelIo.save(sJson, function(blob) {
  86. saveAs(blob, file);
  87. _removeHideSpread(div);
  88. });
  89. };
  90. const exportSimpleXlsxSheets = function (sheets, file) {
  91. if (!sheets || sheets.length === 0) return;
  92. const div = _createHideSpread();
  93. const spread = new spreadNS.Workbook(div, {sheetCount: sheets.length});
  94. for (const [i, sheetData] of sheets.entries()) {
  95. const sheet = spread.getSheet(i);
  96. sheet.name(sheetData.name);
  97. exportSimpleXlsxSheetData(sheet, sheetData.setting, sheetData.data);
  98. }
  99. const excelIo = new GC.Spread.Excel.IO();
  100. const sJson = JSON.stringify(spread.toJSON());
  101. excelIo.save(sJson, function(blob) {
  102. saveAs(blob, file);
  103. _removeHideSpread(div);
  104. });
  105. };
  106. const exportSpread2XlsxWithHeader = function (spread, file) {
  107. spread.getActiveSheet().options.isProtected = false;
  108. const excelIo = new GC.Spread.Excel.IO();
  109. const sJson = JSON.stringify(spread.toJSON({columnHeadersAsFrozenRows: true, rowHeadersAsFrozenColumns: true}));
  110. excelIo.save(sJson, function(blob) {
  111. saveAs(blob, file);
  112. });
  113. spread.getActiveSheet().options.isProtected = true;
  114. };
  115. return {exportSimpleXlsxSheet, exportSpread2XlsxWithHeader, exportSimpleXlsxSheets}
  116. })();
  117. const XLSXObj = (function () {
  118. const transportSheetData = function (setting, data) {
  119. const headerStyle = {
  120. font: { sz: 10, bold: true },
  121. alignment: { horizontal: 'center' },
  122. };
  123. const sHeader = setting.header
  124. .map((v, i) => Object.assign({}, {v: v, s: headerStyle, position: String.fromCharCode(65+i) + 1 }))
  125. .reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v, s: next.s}}), {});
  126. const sData = data
  127. .map((v, i) => v.map((k, j) => Object.assign({}, {
  128. v: k ? k : '',
  129. s: { font: { sz: 10 }, alignment: {horizontal: setting.hAlign[j]}},
  130. position: String.fromCharCode(65+j) + (i+2) })))
  131. .reduce((prev, next) => prev.concat(next))
  132. .reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v, s: next.s}}), {});
  133. const output = Object.assign({}, sHeader, sData);
  134. const outputPos = Object.keys(output);
  135. const result = Object.assign({}, output,
  136. {'!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1]},
  137. {'!cols': setting.width.map((w) => Object.assign({}, {wpx: w}))});
  138. return result;
  139. };
  140. const exportXlsxSheet = function (setting, data, file) {
  141. const result = transportSheetData(setting, data);
  142. const xlsxData = {
  143. SheetNames: ['Sheet1'],
  144. Sheets: {
  145. 'Sheet1': result
  146. }
  147. };
  148. const blob = xlsxUtils.format2Blob(xlsxData);
  149. saveAs(blob, file);
  150. };
  151. const exportXlsxSheets = function (sheets, file) {
  152. const xlsxData = { SheetNames: [], Sheets: {} };
  153. for (const sheet of sheets) {
  154. const xlsxSheet = transportSheetData(sheet.setting, sheet.data);
  155. xlsxData.SheetNames.push(sheet.name);
  156. xlsxData.Sheets[sheet.name] = xlsxSheet;
  157. }
  158. const blob = xlsxUtils.format2Blob(xlsxData);
  159. saveAs(blob, file);
  160. };
  161. return { exportXlsxSheet, exportXlsxSheets }
  162. })();