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