利用xlsx库导出页面表格为xlsx文件

发布时间 2023-07-09 18:50:52作者: 转眼春夏秋冬如烟

html

<el-table id="table"></el-table>

js

import * as XLSX from 'xlsx'
/**
	* 导出表格为xlsx文件
	* @param { string } id 表格dom的id
*/
async function exportTable(id, fileName){
    let wb = XLSX.utils.table_to_book(document.getElementById(id))
    let wbout = XLSX.write(wb, {
        bookType: 'xlsx',
		bookSST: true,
        type: 'array'
    })
    try{
        XLSX.utils.book_append__sheet(wb, wbout)
        XLSX.writeFile(wb, `${fileName}.xlsx`)
    }catch(e){
        console.log(e)
    }
}