html导出excel

function base64 (content) {
return window.btoa(unescape(encodeURIComponent(content)))
}
 
function  exportTab () { // 导出
let tath = this
let table = document.getElementById('tableID') //tableID为dom层 table  ID
let excelContent = table.innerHTML
let excelFile = "<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'>"
excelFile += '<head><!--[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>'
excelFile += '<body><table>'
excelFile += excelContent
excelFile += '</table></body>'
excelFile += '</html>'
let link = 'data:application/vnd.ms-excel;base64,' + tath.base64(excelFile)
let a = document.createElement('a')
a.download = tath.tabtitle + '课时计划表' + '.xlsx'
a.href = link
a.click()
}
posted @ 2019-09-30 16:16  土豆不豆  阅读(256)  评论(0编辑  收藏  举报