安装依赖

npm install file-saver xlsx -S

然后在需要的页面中引入依赖包

import FileSaver from 'file-saver';
import XLSX from 'xlsx';

导出方法:

// 导出
handle_export() {
    var xlsxParam = {
        raw: true
    }; //转换成excel时,使用原始的格式,这样导出的时候数字过长不会变成科学计数法
    let workbook = XLSX.utils.book_new();
    let ws1 = XLSX.utils.table_to_sheet(document.querySelector('#table1'), xlsxParam);
    // 这里可以添加多个sheet页
    XLSX.utils.book_append_sheet(workbook, ws1, 'table1');
    let ws2 = XLSX.utils.table_to_sheet(document.querySelector('#table2'), xlsxParam);
    XLSX.utils.book_append_sheet(workbook, ws2, 'table2');
    let ws3 = XLSX.utils.table_to_sheet(document.querySelector('#table3'), xlsxParam);
    XLSX.utils.book_append_sheet(workbook, ws3, 'table3');
    let wbout = XLSX.write(workbook, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
    });
    try {
        FileSaver.saveAs(
        new Blob([wbout], {
            type: 'application/octet-stream;charset=utf-8"'
        }), '多sheet演示.xlsx');
    } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbOut);
    }
    return wbout;
}

Logo

前往低代码交流专区

更多推荐