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