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');

转载于:https://www.cnblogs.com/shiyujian/p/10038150.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值