<html>
<head>
<button onclick='exportExcel()'>导出</button>
</head>
<body>
<script>
const exportExcel = () => {
// 要导出的数据
const list = [{
name: '张山',
phone: '123456789',
email: '123456789@qq.com'
},
{
name: '李四',
phone: '123456789',
email: '123456789@qq.com'
},
{
name: '王二',
phone: '123456789',
email: '123456789@qq.com'
},
{
name: '麻子',
phone: '123456789',
email: '123456789@qq.com'
},
];
// 列标题,逗号隔开,每一个逗号就是隔开一个单元格
let str = `姓名,电话,邮箱\n`;
// 增加\t为了不让表格显示科学计数法或者其他格式
for (let i = 0; i < list.length; i++) {
for (const key in list[i]) {
str += `${list[i][key] + '\t'},`;
}
str += '\n';
}
// encodeURIComponent解决中文乱码
const uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
// 通过创建a标签实现
const link = document.createElement("a");
link.href = uri;
// 对下载的文件命名
link.download = "数据表.csv";
link.click();
}
</script>
</body>
</html>
10-24
424
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)