<template>
<div>
<el-button
@click="exportExcel"
style="margin-top: 2px;"
size="medium"
type="success"
>导出</el-button>
</div>
</template>
<script>
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default {
props: ["idName", "excleTitle"],
data() {
return {};
},
mounted() {
console.log(this);
},
methods: {
//传入一个 table表格的idName 再传入一个Excel的标题
exportExcel() {
console.log(this.idName, this.excleTitle);
/* generate workbook object from table */
let wb = XLSX.utils.table_to_book(
document.querySelector(`#${this.idName}`)
);
/* get binary string as output */
let wbout = XLSX.write(wb, {
bookType: "xlsx",
bookSST: true,
type: "array"
});
try {
FileSaver.saveAs(
new Blob([wbout], { type: "application/octet-stream" }),
`${this.excleTitle}.xlsx`
);
} catch (e) {
if (typeof console !== "undefined") console.log(e, wbout);
}
return wbout;
}
}
};
</script>
08-30
291
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-23
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交