Home > Web Front-end > JS Tutorial > Export table data to excel file in js

Export table data to excel file in js

小云云
Release: 2018-05-25 16:48:48
Original
3106 people have browsed it

This article mainly shares with you some code and examples of how to export table data to excel files in js. I hope it can help you.

Convert the table to excel and download
##

(document).ready(function () {
(“#myBtn”).click(function () {    //点击下载按钮,执行方法 
                CreateExcel(“myTable”,”test”); 
            }); 
        }); 
        //将table导出到excel 
        var idTmr; 
        function getExplorer() {     //返回浏览器类型 
            var explorer = window.navigator.userAgent; 
            //ie 
            if (explorer.indexOf(“MSIE”) >= 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 CreateExcel(tableid,fileName) { 
            if (getExplorer() == ‘ie’) { 
                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(fileName + “.xls”,  //文件名和文件格式  但尝试改了一下fileName 这里并不影响 
                        “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,fileName)    //调用tableToExcex   table的id 和 要生成的文件名            } 
        } 
        function Cleanup() { 
            window.clearInterval(idTmr); 
            CollectGarbage(); 
        } 
        var tableToExcel = (function () { 
            var uri = ‘data:application/vnd.ms-excel;base64,’, 
                template = 
                    ‘
Copy after login

{ table}

’, 
                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)) 
                a = document.createElement(“a”); 
                a.download = name; 
                a.href = uri + base64(format(template, ctx)); 
                document.body.appendChild(a); 
                a.click(); 
                document.body.removeChild(a); 
            } 
        })()
Copy after login

Download/Download excel


Context table layoutProductPayment DateStatusProduct 123/11/2013To be shippedProduct 210/11/2013ShippingProduct 3 20/10/2013To be confirmedProduct 420/10/2013Returned
Related recommendations:

JS export Excel table exceeds 26 English characters solution ES6

php use Native method to export excel instance sharing

PHP class library usage example to export excel data

The above is the detailed content of Export table data to excel file in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template