方案一:vue-json-excel
cnpm i -S vue-json-excel
import JsonExcel from "vue-json-excel";
export default {
components: { JsonExcel },
}
<JsonExcel :fields="fields" :data="data" title="导出标题" defaultValue="-"> 导出 </JsonExcel>
export default {
components: { JsonExcel },
data() {
return {
name:"导出的文件名.xls",
fields:{
"姓名":"name",
"年龄":"age"
},
data:[
{name:"张三",age:"18"},
{name:"李四",age:"19"},
{name:"赵五",age:"20"},
]
};
},
};
属性名 | 类型 | 备注 |
---|
data | array | 导出的数据 | fields | object | 设置要导出的字段,没有设置按data数据导出 | defaultValue | string | 导出项数据为空时显示的信息 | type | string | 导出的文件后缀(xls,csv)默认xls | name | string | 导出的文件名 | title | string | 表格第一行标题 |
更多配置可前往https://www.npmjs.com/package/vue-json-excel
方案二:xlsx
cnpm i -S xlsx
const srcData = [
["name", "gender", "age"],
["zhangsan", "male", 23],
["lisi", "female", 24],
["wangwu", "male", 25],
];
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet(srcData);
ws["!merges"] = [
{ s: { r: 0, c: 0 }, e: { r: 0, c: 2 } },
];
XLSX.utils.book_append_sheet(wb, ws, "sheet1");
XLSX.writeFile(wb, "fileName.xls");
|