只需要你调用下面的方法,传入一个表格元素id,加一个文件名就行
function tableToXlsx(id, filename) {
if(!filename) filename = document.querySelector('title').innerText || ''
let table = ''
if(document.getElementById(id)){
table = document.getElementById(id)
}else{
table = document.querySelector('table')
}
const uri = 'data:application/vnd.ms-excel;base64,',
template = `<html><head><meta charset="UTF-8"></head><body><table border="1" style="text-align: center;">
${table.innerHTML}</table></body></html>`;
const a = document.createElement('a');
document.body.appendChild(a);
a.href = uri + window.btoa(unescape(encodeURIComponent(template)));
a.download = filename;
a.click();
document.body.removeChild(a);
}