1安装插件
npm install --save xlsx file-saver
2在文件中引用
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
3代码实现
<template>
<div>
<div class="index_search">
<el-button
size="mini"
type="success"
@click="outExcel"
>导出</el-button>
</div>
<el-table
id="outtable"
:data="tableData"
style="width: 100%"
stripe
border
show-header
height="73vh"
highlight-current-row
:header-cell-style="{ background: '#eef1f6', color: '#606266' }"
>
<el-table-column
type="index"
align="center"
width="60"
label="序号"
></el-table-column>
</el-table>
</div>
</template>
<script>
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default {
data() {
return {
tableData: [],//表格数据
};
},
methods: {
//导出
outExcel() {
/* out-table关联导出的dom节点 */
var wb = XLSX.utils.table_to_book(document.querySelector("#outtable"));
/* get binary string as output */
var wbout = XLSX.write(wb, {
bookType: "xlsx",
bookSST: true,
type: "array"
});
try {
FileSaver.saveAs(
new Blob([wbout], { type: "application/octet-stream" }),
"表格名称.xlsx"
);
} catch (e) {
if (typeof console !== "undefined") console.log(e, wbout);
}
return wbout;
}
}
};
</script>