安装插件:
npm i js-table2excel
引入插件:
import table2excel from "js-table2excel";
table收集数据:
<el-table
size="small"
ref="multipleTableRef"
:header-cell-style="{ background: '#f5f6fa', color: '#333333' }"
:data="tableData"
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" width="55" />
<el-table-column property="name" label="名称" />
<el-table-column property="img" label="图片" />
</el-table>
const handleSelectionChange = (val: User[]) => {
//收集选中的数据
multipleSelection.value = val;
};
导出excel文件:
let arr = [
{
title: "名称", //名字自定义
key: "name", //对应table表格里的property
type: "text", //文字类型
},
{
title: "图片",
key: "img",
type: 'image', //图片类型
width: 150,
height: 150,
},
];
// 导出
const onImportTable = () => {
//判断有没有选中数据
if (multipleSelection.value.length <= 0)
return ElMessage.warning("请先选择要导出的数据");
table2excel(
arr,
multipleSelection.value,
'excel表名称'}`
);
};