首页 > 代码库 > 原生JavaScript 导出excel表格(兼容ie和其他主流浏览器)

原生JavaScript 导出excel表格(兼容ie和其他主流浏览器)

因同事的需求是想前端导出excel表格,网上找了一些demo,自己修改了一下,可能以后会用到,记录下来吧,兼容ie和一些主流浏览器,ie可能会报错,原因参考 这里,edge 浏览器还没有办法导出,正在尝试...

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>table 导出excel表格</title>
    </head>
    <body>
        <div>  
            <button type="button" onclick="exportExcel(‘tableExcel‘)">导出Excel</button>  
        </div> 
        <div id="myDiv">  
            <table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">  
                <tr>  
                    <td colspan="5" align="center">html 表格导出道Excel</td>  
                </tr>  
                <tr>  
                    <td>列标题1</td>  
                    <td>列标题2</td>  
                    <td>类标题3</td>  
                    <td>列标题4</td>  
                    <td>列标题5</td>  
                </tr>  
                <tr>  
                    <td>aaa</td>  
                    <td>bbb</td>  
                    <td>ccc</td>  
                    <td>ddd</td>  
                    <td>eee</td>  
                </tr>  
                <tr>  
                    <td>AAA</td>  
                    <td>BBB</td>  
                    <td>CCC</td>  
                    <td>DDD</td>  
                    <td>EEE</td>  
                </tr>  
                <tr>  
                    <td>FFF</td>  
                    <td>GGG</td>  
                    <td>HHH</td>  
                    <td>III</td>  
                    <td>JJJ</td>  
                </tr>  
            </table>  
        </div>  

        <script>
            var idTmr;  
              function  getExplorer() {  
                var explorer = window.navigator.userAgent;
                console.log(explorer); 
                //ie  
                if (explorer.indexOf("Trident") >= 0) {  
                    return ‘ie‘;  
                }  
                //firefox  
                else if (explorer.indexOf("Firefox") >= 0) {  
                    return ‘Firefox‘;  
                }  
                //Chrome  
                else if(explorer.indexOf("Chrome") >= 0){  
                    return ‘Chrome‘;  
                }  
                //Opera  
                else if(explorer.indexOf("Opera") >= 0){  
                    return ‘Opera‘;  
                }  
                //Safari  
                else if(explorer.indexOf("Safari") >= 0){  
                    return ‘Safari‘;  
                }
            } 

            function exportExcel(tableid) {  
                if(getExplorer()==‘ie‘){  
                    var curTbl = document.getElementById(tableid);  
                    var oXL = new ActiveXObject("Excel.Application");  
                    var oWB = oXL.Workbooks.Add();  
                    var oSheet = oWB.ActiveSheet;  
                    var sel = document.body.createTextRange();  
                    sel.moveToElementText(curTbl);  
                    sel.select();  
                    sel.execCommand("Copy");  
                    oSheet.Paste();  
                    oXL.Visible = true; 
                }  
                else{  
                    tableToExcel(tableid)  
                }  
            }   

            var tableToExcel = (function() {  
                var uri = ‘data:application/vnd.ms-excel;base64,‘,  
                        template = ‘<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>‘,  
                        base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },  
                        format = function(s, c) {  
                            return s.replace(/{(\w+)}/g,  
                                    function(m, p) { return c[p]; }) }  
                return function(table, name) {  
                    if (!table.nodeType) table = document.getElementById(table)  
                    var ctx = {worksheet: name || ‘Worksheet‘, table: table.innerHTML}  
                    window.location.href = uri + base64(format(template, ctx))  
                }  
            })(); 
        </script>
    </body>
</html>

原文来自: 这里

原生JavaScript 导出excel表格(兼容ie和其他主流浏览器)