vue+file-saver+xlsx导出table为excel

1、安装:

npm install file-saver xlsx --save-dev
cnpm install file-saver xlsx --save-dev //淘宝镜像

2、引入:

import FileSaver from 'file-save';

import XLSX from 'xlsx';

3、点击事件:      

exportExcel() {

        let vb = XLSX.utils.table_to_book(document.getElementById('outTable'));

        let vbout = XLSX.write(vb, {bookType: 'xlsx', bookSST: true, type: 'array'});

        try {

            FileSaver.saveAs(new Blob([vbout], {type: 'application/octet-stream'}), 'sheetjs.xlsx');
        } catch (e) {
            if (typeof console !== 'undefined') console.log(e, vbout);
        }
        return vbout;
      }

你可能感兴趣的:(vue,js)