vue下载excel

我只是一个虾纸丫 提交于 2020-04-05 18:44:18

使用'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'];

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!