vue ElementUI导出多个sheet

vue ElementUI导出多个sheet

  • 1.安装依赖
  • 2.在组件中导入
  • 3.在组件中methods写入一下方法
  • 4.button绑定方法

1.安装依赖

npm install  xlsx file-saver  --save

2.在组件中导入

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

3.在组件中methods写入一下方法

outputFile() {
        var workbook = XLSX.utils.book_new();

        /* convert table 'table1' to worksheet named "Sheet1" */
        var ws1 = XLSX.utils.table_to_sheet(document.querySelector('#Table1'));
        XLSX.utils.book_append_sheet(workbook, ws1, "sheet1");

        /* convert table 'table2' to worksheet named "Sheet2" */
        var ws2 = XLSX.utils.table_to_sheet(document.querySelector('#Table2'));
        XLSX.utils.book_append_sheet(workbook, ws2, "sheet2");

        /* get binary string as output */
        var wbOut = XLSX.write(workbook, {
          bookType: "xlsx",
          bookSST: true,
          type: "array"
        });
        try {
          FileSaver.saveAs(
                  new Blob([wbOut], { type: "application/octet-stream" }),
                  "Demo.xlsx"
          );
        } catch (e) {
          if (typeof console !== "undefined") console.log(e, wbOut);
        }
        return wbout;
      }

4.button绑定方法

保存

使用方法参考

SheetJs
FileSaver

你可能感兴趣的:(vue)