1. 程式人生 > >vue+file-saver+xlsx匯出table為excel

vue+file-saver+xlsx匯出table為excel

1、安裝:npm 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;

      }