xlsx模塊的使用---導出excel


導入

cnpm install --save xlsx

引入

import XLSX from 'xlsx';

使用

_headers為列頭數組      tblData為數據數組,每一個對象為一行數據

let _headers = ['日期', ...xAxisData];   
let headers = _headers.map((v, i) => Object.assign({}, { v: v, position: String.fromCharCode(65 + i) + 1 }))
.reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v}}), {});
console.log('headers', headers);
let testttt = tblData.map((v, i) => _headers.map((k, j) => Object.assign({}, { v: v[k], position: String.fromCharCode(65 + j) + (i + 2) })))
.reduce((prev, next) => prev.concat(next))
.reduce((prev, next) => Object.assign({}, prev, {[next.position]: {v: next.v}}), {});
console.log('testttt', testttt);
let output = Object.assign({}, headers, testttt);
console.log('output', output);
// 獲取所有單元格的位置
let outputPos = Object.keys(output);
console.log('outputPos', outputPos);
// 計算出范圍
let ref = outputPos[0] + ':' + outputPos[outputPos.length - 1];
console.log('ref', ref);
// 構建 workbook 對象
let wb = {
SheetNames: ['mySheet'],
Sheets: {
'mySheet': Object.assign({}, output, { '!ref': ref })
}
};
XLSX.writeFile(wb, 'output.xlsx');


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM