<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="downloadExcel()">下载excel</button>
<script>
const download = (data, name) => {
const a = document.createElement('a')
var dataType = "\uFEFF";
dataType += (["ID", "姓名", "年龄"].join(','));
dataType += '\n';
data.forEach(v => {
v = { id: v.id, name: decodeURIComponent(v.name), age: v.age }
dataType += (Object.values(v).join(','));
dataType += '\n';
})
var t4 = new Blob([dataType], {
type: 'text/csv'
});
a.href = URL.createObjectURL(t4)
a.setAttribute('download', name)
a.click()
}
const downloadExcel = () => {
download([
{ id: '1', name: "小杰", age: "20" },
{ id: '2', name: "小明", age: "25" },
{ id: '3', name: "小怡", age: "18" }
], '用户信息.xls')
}
</script>
</body>
</html>