You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
'use strict';
|
|
const xlsx = require('better-xlsx');
|
|
const path = require('path')
|
|
const moment = require('moment')
|
|
|
|
module.exports = function (app, opts) {
|
|
async function simpleExcelDown ({ data = [], header = [], fileName = moment().format('YYYY-MM-DD HH:mm:ss') } = {}) {
|
|
const fileDirPath = path.join(__dirname, `../../downloadFiles`)
|
|
const file = new xlsx.File();
|
|
const sheet_1 = file.addSheet('sheet_1');
|
|
|
|
// header
|
|
const headerRow = sheet.addRow();
|
|
for (let h of header) {
|
|
const cell = headerRow.addCell();
|
|
cell.value = h.title;
|
|
|
|
//设置文本在单元格内水平垂直居中
|
|
cell.style.align.h = 'center';
|
|
cell.style.align.v = 'center';
|
|
|
|
// border(cell, 0, 0, 1, 0)
|
|
}
|
|
|
|
const savePath = path.join(fileDirPath, fileName + '.xlsx')
|
|
await new Promise(function (resolve, reject) {
|
|
file.saveAs()
|
|
.pipe(fs.createWriteStream(savePath))
|
|
.on('finish', () => {
|
|
resolve()
|
|
});
|
|
})
|
|
return savePath
|
|
}
|
|
|
|
return {
|
|
simpleExcelDown
|
|
}
|
|
}
|