当先锋百科网

首页 1 2 3 4 5 6 7

需要先安装xlsx file-save

npm install --save xlsx file-saver
<template>
	<div>
		<el-table ref="report-table" :data="tableData">
		</el-table>
	</idv>
</template>

import * as XLSX from "xlsx";
import FileSaver from 'file-saver';

methods: {
	exportExcel() {
      try {
        const $e = this.$refs['report-table'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }

        const wb = XLSX.utils.table_to_book($table, {raw: true})
        const wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: true, type: 'array'})
        FileSaver.saveAs(
          new Blob([wbout], {type: 'application/octet-stream'}),
          `统计.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    }
}