首页 >web前端 >js教程 >用js创建excel文档

用js创建excel文档

不言
不言原创
2018-04-26 14:29:362511浏览

本篇文章给大家介绍的就是用js创建excel文档 的纯代码,有需要的朋友可以来看一下

<button type="button" onclick="method5(&#39;tableid&#39;)">导出Excel方法五</button>//按钮

<table id=&#39;tableid&#39;>//tableid
    <tr>
       <td>1111</td>
        <td>2222</td>
        <td>3333</td>
        <td>4444</td>
    </tr>

<tr>
       <td>aaaa</td>
        <td>bbbb</td>
        <td>cccc</td>
        <td>dddd</td>
    </tr>

</table>
var idTmr;  
        function  getExplorer() {  
            var explorer = window.navigator.userAgent ;  
            //ie  
            if (explorer.indexOf("MSIE") >= 0) {  
                return &#39;ie&#39;;  
            }  
            //firefox  
            else if (explorer.indexOf("Firefox") >= 0) {  
                return &#39;Firefox&#39;;  
            }  
            //Chrome  
            else if(explorer.indexOf("Chrome") >= 0){  
                return &#39;Chrome&#39;;  
            }  
            //Opera  
            else if(explorer.indexOf("Opera") >= 0){  
                return &#39;Opera&#39;;  
            }  
            //Safari  
            else if(explorer.indexOf("Safari") >= 0){  
                return &#39;Safari&#39;;  
            }  
        }  
        function method5(tableid) {  
            if(getExplorer()==&#39;ie&#39;)  
            {  
                var curTbl = document.getElementById(tableid);  
                var oXL = new ActiveXObject("Excel.Application");  
                var oWB = oXL.Workbooks.Add();  
                var xlsheet = oWB.Worksheets(1);  
                var sel = document.body.createTextRange();  
                sel.moveToElementText(curTbl);  
                sel.select();  
                sel.execCommand("Copy");  
                xlsheet.Paste();  
                oXL.Visible = true;  
  
                try {  
                    var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");  
                } catch (e) {  
                    print("Nested catch caught " + e);  
                } finally {  
                    oWB.SaveAs(fname);  
                    oWB.Close(savechanges = false);  
                    oXL.Quit();  
                    oXL = null;  
                    idTmr = window.setInterval("Cleanup();", 1);  
                }  
  
            }  
            else  
            {  
                tableToExcel(tableid)  
            }  
        }  
        function Cleanup() {  
            window.clearInterval(idTmr);  
            CollectGarbage();  
        }  
        var tableToExcel = (function() {  
            var uri = &#39;data:application/vnd.ms-excel;base64,&#39;,  
                    template = &#39;<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>&#39;,  
                    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 || &#39;Worksheet&#39;, table: table.innerHTML}  
                window.location.href = uri + base64(format(template, ctx))  
            }  
        })()

相关推荐:

js创建一个Map代码分享

JS创建html浏览器导出下载的方法

D3.js创建物流地图

以上是用js创建excel文档的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn