码农之路

JS实现页面table导出到excel

2020-05-17  本文已影响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();
    }
} 
上一篇下一篇

猜你喜欢

热点阅读