准备持续更新工作中遇到的问题...
最近学到了一个原生table转excel表格并导出功能,实际的代码很少。还是我发沸点,有人用gpt给我的答案,哈哈哈哈哈非常感谢。
// html部分
<table
border="1"
cellpadding='0'
cellspacing='0'
id="myTable"
>
<tr>
<th>上级对口机构</th>
<th>市安全生产委员会</th>
<td>111</td>
<th>市突发事件应急委员会</th>
<td>222</td>
</tr>
</table>
// js部分
function exportToExcel () {
const table = document.getElementById('myTable'); // 原生table的id名
const html = table.outerHTML;
const blob = new Blob([html], { type: 'application/vnd.ms-excel' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'excel.xls'; // excel表格名称
a.click();
window.URL.revokeObjectURL(url);
}
还可以导出单多选,使用特殊符号就行
注意如果实际html结构不满足要求,可以写一个满足业务需求的独立隐藏html。(我用的v-show="false"写一个结构并导出)