'use strict'; /** * * * @author Mai * @date * @version */ const SpreadExcelObj = (function() { const _createHideSpread = function () { const div = document.createElement("div"); div.setAttribute('id', 'exportExcelSpread'); //$(div).css("position", "absolute").css('top', 0).css('left', 0).css('height', 300).css('width', 500).css('z-index', 999); //document.body.insertBefore(div, null); div.style.display = 'none'; return div; }; const _removeHideSpread = function (div) { document.body.removeChild(div); }; const exportSpread2XlsxWithHeader = function (spread, file) { spread.getActiveSheet().options.isProtected = false; const excelIo = new GC.Spread.Excel.IO(); const sJson = JSON.stringify(spread.toJSON({columnHeadersAsFrozenRows: true, rowHeadersAsFrozenColumns: true})); excelIo.save(sJson, function(blob) { saveAs(blob, file); }); spread.getActiveSheet().options.isProtected = true; }; const exportSimpleXlsxSheet = function (setting, data, file) { const div = _createHideSpread(); const spread = SpreadJsObj.createNewSpread(div, true); const sheet = spread.getActiveSheet(); SpreadJsObj.beginMassOperation(sheet); sheet.options.isProtected = false; sheet.setColumnCount(setting.cols.length); sheet.setRowCount(setting.headRows + data.length); for (let iRow = 0; iRow < setting.headRowHeight.length; iRow++) { sheet.setRowHeight(iRow, setting.headRowHeight[iRow]); } for (let iCol = 0; iCol < setting.cols.length; iCol++) { const col = setting.cols[iCol]; const title = col.title.split('|'); const colSpan = col.colSpan ? col.colSpan.split('|') : ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|') : ['1']; for (let i = 0; i < title.length; i++) { const cell = sheet.getCell(i, iCol); cell.text(title[i]).wordWrap(true).hAlign(1).vAlign(1).font(setting.headerFont); if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) { sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i])); } } sheet.setColumnWidth(iCol, col.width); if (col.visible !== undefined && col.visible !== null) { sheet.setColumnVisible(iCol, col.visible); } } for (let iRow = 0; iRow < data.length; iRow++) { const curRow = setting.headRows + iRow; const d = data[iRow]; for (let iCol = 0; iCol < setting.cols.length; iCol++) { const cell = sheet.getCell(curRow, iCol); const col = setting.cols[iCol]; if (col.field !== '' && d[col.field]) { cell.value(d[col.field]); if (typeof d[col.field] === 'string') { cell.formatter('@'); } } if (col.font) { cell.font(col.font); } else if (setting.font) { cell.font(setting.font); } cell.hAlign(col.hAlign); } } SpreadJsObj.endMassOperation(sheet); const excelIo = new GC.Spread.Excel.IO(); const sJson = JSON.stringify(spread.toJSON()); excelIo.save(sJson, function(blob) { saveAs(blob, file); _removeHideSpread(div); }); }; return {exportSimpleXlsxSheet, exportSpread2XlsxWithHeader} })(); const XLSXObj = (function () { const exportXlsxSheet = function (setting, data, file) { const headerStyle = { font: { sz: 10, bold: true }, alignment: {horizontal: 'center'}, }; const sHeader = setting.header .map((v, i) => Object.assign({}, {v: v, s: headerStyle, position: String.fromCharCode(65+i) + 1 })) .reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v, s: next.s}}), {}); const sData = data .map((v, i) => v.map((k, j) => Object.assign({}, { v: k ? k : '', s: { font: { sz: 10 }, alignment: {horizontal: setting.hAlign[j]}}, position: String.fromCharCode(65+j) + (i+2) }))) .reduce((prev, next) => prev.concat(next)) .reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v, s: next.s}}), {}); const output = Object.assign({}, sHeader, sData); const outputPos = Object.keys(output); const result = Object.assign({}, output, {'!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1]}, {'!cols': setting.width.map((w) => Object.assign({}, {wpx: w}))}); const xlsxData = { SheetNames: ['Sheet1'], Sheets: { 'Sheet1': result } }; const blob = xlsxUtils.format2Blob(xlsxData); saveAs(blob, file); }; return {exportXlsxSheet} });