美文网首页码农之路
JS实现页面table导出到excel

JS实现页面table导出到excel

作者: 文心武士 | 来源:发表于2020-05-17 09:49 被阅读0次

示例代码:

function tableToExcel() {
    var tbody = '<tr><th>序号</th><th>说明<th/></tr><tr><td>1</td><td>测试</td></tr>';
    var html="<html><head><meta charset='UTF-8'></head><body><table>"+tbody+"</table></body></html>"
    var excelBlob = new Blob([html], {type: 'application/vnd.ms-excel'});
    var fileName = "文件名.xls";
    if(!!window.ActiveXObject || "ActiveXObject" in window){
        //IE浏览器
        window.navigator.msSaveOrOpenBlob(excelBlob,fileName);
    }else{
        var oa = document.createElement('a');
        oa.href = URL.createObjectURL(excelBlob);
        oa.download = fileName;
        document.body.appendChild(oa);
        oa.click();
    }
} 

相关文章

网友评论

    本文标题:JS实现页面table导出到excel

    本文链接:https://www.haomeiwen.com/subject/jtyjohtx.html