function tableToExcel(jsonData) {
//要导出的json数据
// const jsonData = [
// {
// name: '甲',
// phone: '123456',
// email: '123@123456.com'
// },
// {
// name: '乙',
// phone: '123456',
// email: '123@123456.com'
// },
// {
// name: '丙',
// phone: '123456',
// email: '123@123456.com'
// },
// {
// name: '丁',
// phone: '123456',
// email: '123@123456.com'
// },
// ]
//列标题
let str = '<tr><td>序号</td><td>站点</td><td>降雨量</td></tr>';
//循环遍历,每行加入tr标签,每个单元格加td标签
for (let i = 0; i < jsonData.length; i++) {
str += '<tr>';
for (let item in jsonData[i]) {
//增加\t为了不让表格显示科学计数法或者其他格式
str += `<td>${jsonData[i][item] + '\t'}</td>`;
}
str += '</tr>';
}
//Worksheet名
let worksheet = 'Sheet1'
let uri = 'data:application/vnd.ms-excel;base64,';
//下载的表格模板数据
let template = `<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
<x:Name>${worksheet}</x:Name>
<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
</head><body><table>${str}<\/table><\/body><\/html>`;
//下载模板
console.log(uri + base64(template));
window.location.href = uri + base64(template)
}
//输出base64编码
function base64(s) { return window.btoa(unescape(encodeURIComponent(s))) }