Home >Web Front-end >JS Tutorial >Create excel document using js

Create excel document using js

不言
不言Original
2018-04-26 14:29:362502browse

This article introduces to you the pure code for creating excel documents using js. Friends in need can take a look

<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))  
            }  
        })()

Related recommendations:

js creates an Map code sharing

How to create html browser export and download with JS

Create logistics map with D3.js

The above is the detailed content of Create excel document using js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn