|
|
@@ -1,6 +1,7 @@
|
|
|
/**
|
|
|
* Created by zhuth on 2019/08/22.
|
|
|
*/
|
|
|
+
|
|
|
const XlsxPopulate = require('xlsx-populate');
|
|
|
|
|
|
;exports = module.exports = (function () {
|
|
|
@@ -8,39 +9,56 @@ const XlsxPopulate = require('xlsx-populate');
|
|
|
function Exportor() {};
|
|
|
|
|
|
function Cell(str) {
|
|
|
+ var me = this;
|
|
|
str = str ? (str + '').toUpperCase() : 'A1';
|
|
|
this._colIdx = str.split(/\d+/)[0]; // 'A'
|
|
|
- this._colIdxNum = this._colIdx.length === 1 ? this._colIdx.charCodeAt() : NaN; // 暂不支持超出Z
|
|
|
+
|
|
|
+ var chArr = this._colIdx.split('');
|
|
|
+ this._colIdxNum = 1;
|
|
|
+ chArr.forEach(function(c, i) {
|
|
|
+ me._colIdxNum += (c.charCodeAt() - 64) * (Math.pow(26, chArr.length - i - 1)); // 1
|
|
|
+ });
|
|
|
+
|
|
|
this._rowIdx = +str.split(/[a-z | A-Z]+/)[1]; // 1
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
Cell.prototype.getRowIdx = function() {
|
|
|
return this._rowIdx;
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
Cell.prototype.getColIdx = function() {
|
|
|
return this._colIdx;
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
Cell.prototype.getColIdxNumber = function() {
|
|
|
return this._colIdxNum;
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
Cell.prototype.toString = function() {
|
|
|
return this._colIdx + this._rowIdx;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
Cell.prototype.addCol = function(num) {
|
|
|
num = +num;
|
|
|
- if(this._colIdxNum + num < 91) {
|
|
|
- this._colIdxNum = this._colIdxNum + num;
|
|
|
- this._colIdx = String.fromCharCode(this._colIdxNum);
|
|
|
- return this;
|
|
|
- }else {
|
|
|
- throw new Error({message: 'error...'});
|
|
|
- }
|
|
|
+ var ch = [];
|
|
|
+ this._colIdxNum = this._colIdxNum + num;
|
|
|
+ var s = this._colIdxNum;
|
|
|
+ do {
|
|
|
+ var x = ~~(s / 26);
|
|
|
+ var y = s % 26;
|
|
|
+ if(y === 0) {
|
|
|
+ ch.unshift('Z');
|
|
|
+ s = x - 1;
|
|
|
+ }else {
|
|
|
+ ch.unshift(String.fromCharCode(64 + y));
|
|
|
+ s = x;
|
|
|
+ }
|
|
|
+ }while(s > 0);
|
|
|
+
|
|
|
+ this._colIdx = ch.join('');
|
|
|
+ return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
Cell.prototype.addRow = function(num) {
|
|
|
num = +num;
|
|
|
this._rowIdx += num;
|
|
|
@@ -78,7 +96,8 @@ const XlsxPopulate = require('xlsx-populate');
|
|
|
clen = columns.length,
|
|
|
rows = sheetData.rows,
|
|
|
sheet,
|
|
|
- cell = new Cell('A1'); // 当前第一列
|
|
|
+ cell = new Cell('A1'),
|
|
|
+ r, startCell, endCell, columnDatas, sheet_columns; // 当前第一列
|
|
|
|
|
|
if(workbook.sheets().length - 1 < index) {
|
|
|
sheet = workbook.addSheet(sheetName)
|
|
|
@@ -87,21 +106,21 @@ const XlsxPopulate = require('xlsx-populate');
|
|
|
}
|
|
|
|
|
|
if(header) {
|
|
|
- let startCell = cell,
|
|
|
- endCell = new Cell('A1').addCol(clen - 1);
|
|
|
- let r = sheet.range(startCell + ":" + endCell).merged(true);
|
|
|
+ startCell = cell;
|
|
|
+ endCell = new Cell('A1').addCol(clen - 1);
|
|
|
+ r = sheet.range(startCell + ":" + endCell).merged(true);
|
|
|
r.value(header);
|
|
|
r.style({ fill: '70AD47', fontColor: 'ffffff', bold: true, horizontalAlignment: 'center', bottomBorder: true, bottomBorderColor: 'EAEAEA' });
|
|
|
cell.addRow(1);
|
|
|
}
|
|
|
|
|
|
if(clen > 0) {
|
|
|
- let columnDatas = [columns.map(function(c) {
|
|
|
+ columnDatas = [columns.map(function(c) {
|
|
|
return c.name;
|
|
|
})];
|
|
|
- let r = sheet.range(cell + ":" + new Cell(cell).addCol(clen - 1));
|
|
|
+ r = sheet.range(cell + ":" + new Cell(cell).addCol(clen - 1));
|
|
|
r.value(columnDatas);
|
|
|
- let sheet_columns = r.cells()[0].map(function(c) {
|
|
|
+ sheet_columns = r.cells()[0].map(function(c) {
|
|
|
return c.column();
|
|
|
});
|
|
|
columns.forEach(function(c, i) {
|
|
|
@@ -113,7 +132,7 @@ const XlsxPopulate = require('xlsx-populate');
|
|
|
cell.addRow(1);
|
|
|
}
|
|
|
if(rows.length > 0) {
|
|
|
- let r = sheet.range(cell + ":" + new Cell(cell).addRow(rows.length - 1).addCol(clen - 1));
|
|
|
+ r = sheet.range(cell + ":" + new Cell(cell).addRow(rows.length - 1).addCol(clen - 1));
|
|
|
r.value(rows);
|
|
|
r.style('fill', function(cell, ri, ci, range) {
|
|
|
return (ri&1) === 0 ? 'C6E0B4' : 'A9D08E';
|
|
|
@@ -123,7 +142,6 @@ const XlsxPopulate = require('xlsx-populate');
|
|
|
|
|
|
index++;
|
|
|
}while(!!json.sheets[index]);
|
|
|
- console.log(workbook);
|
|
|
|
|
|
return workbook.outputAsync();
|
|
|
};
|