使用'xlsx、'file-saver'。
import XLSX from 'xlsx';
import {saveAs} from 'file-saver';
import moment from 'moment';
export function exportData(data: any[], header: any[], fileName: string) {
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(data, {header, skipHeader: true});
XLSX.utils.book_append_sheet(wb, ws, 'Sheet 1');
/* bookType can be any supported output type */
const wopts: any = {bookType: 'xlsx', bookSST: false, type: 'array'};
const wbout = XLSX.write(wb, wopts);
const ts = moment().format('YYYYMMDDHHmmss');
/* the saveAs call downloads a file on the local machine */
saveAs(new Blob([wbout], {type: 'application/octet-stream'}), `${fileName}-${ts}.xlsx`);
}
data格式为包括表头在内的数据
const dataList = Concat(
[
{
name: '名称',
date: '日期',
time: '时间',
district: '区域',
point: '选择点位',
type: '出行方式',
display: '展示类型',
oname: '区域O的名称',
dname: '区域D的名称',
rank: '排名',
distance: '距离(米)',
total: '总量(人)',
leave: '出发总量(人)',
arrive: '到达总量(人)',
},
],
dataArr,
);
header数据为按照表头顺序对应的字段名
const header = ['name', 'date', 'time', 'district', 'point', 'type', 'display', 'oname', 'dname', 'rank', 'distance', 'total', 'leave', 'arrive'];
来源:oschina
链接:https://my.oschina.net/u/3589012/blog/3217308