js导出页面上table
先上代码:
// html
<a-table id='tableTemp'>
...
</a-table>
let tables = document.getElementById('tableTemp')
if (tables) {
let exportFileContent = tables.getElementsByTagName('table')[0].outerHTML
// 导出并添加表格样式和表头
exportFileContent = `<html xmlns:x="urn:schemas-microsoft-com:office:excel"><head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:WorksheetOptions><x:Print><x:ValidPrinterInfo /></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml></head>
<style>
table thead tr { height: 60px}
table tbody tr { height: 60px}
</style>
<table><tbody><tr style="height: 80px; font-size: 18px"><th colspan="20">${name}</th></tr></tbody></table>
${exportFileContent}</html>`
let blob = new Blob([exportFileContent], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
var link = window.URL.createObjectURL(blob)
var a = document.createElement('a')
a.download = name + '.xlsx'
a.href = link
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
上次更新: 2025/09/05, 8:09:00