javascript - Unable to export table to excel using tableExport -
i have scripts mentioned , searching have written following html code:
<table id="customers" class="table table-striped" > <thead> <tr class='warning'> <th>country</th> <th>population</th> <th>date</th> <th>%ge</th> </tr> </thead> <tbody> <tr> <td>chinna</td> <td>1,363,480,000</td> <td>march 24, 2014</td> <td>19.1</td> </tr> <tr> <td>india</td> <td>1,241,900,000</td> <td>march 24, 2014</td> <td>17.4</td> </tr> <tr> <td>united states</td> <td>317,746,000</td> <td>march 24, 2014</td> <td>4.44</td> </tr> <tr> <td>indonesia</td> <td>249,866,000</td> <td>july 1, 2013</td> <td>3.49</td> </tr> </tbody> </table> <button onclick ="$('#customers').tableexport({type:'excel',escape:'false'});"> xls</button>
the code nothing when click on button. jsfiddle link is: https://jsfiddle.net/dtchh/12981/ reason of code not working?
you can use jquery ui export excel .
//table2excel.js ;(function ( $, window, document, undefined ) { var pluginname = "table2excel", defaults = { exclude: ".noexl", name: "table2excel" }; // actual plugin constructor function plugin ( element, options ) { this.element = element; this.settings = $.extend( {}, defaults, options ); this._defaults = defaults; this._name = pluginname; this.init(); } plugin.prototype = { init: function () { var e = this; e.template = { head: "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/tr/rec-html40\"><head><!--[if gte mso 9]><xml><x:excelworkbook><x:excelworksheets>", sheet: { head: "<x:excelworksheet><x:name>", tail: "</x:name><x:worksheetoptions><x:displaygridlines/></x:worksheetoptions></x:excelworksheet>" }, mid: "</x:excelworksheets></x:excelworkbook></xml><![endif]--></head><body>", table: { head: "<table>", tail: "</table>" }, foot: "</body></html>" }; e.tablerows = []; // contents of table except exclude $(e.element).each( function(i,o) { var temprows = ""; $(o).find("tr").not(e.settings.exclude).each(function (i,o) { temprows += "<tr>" + $(o).html() + "</tr>"; }); e.tablerows.push(temprows); }); e.tabletoexcel(e.tablerows, e.settings.name); }, tabletoexcel: function (table, name) { var e = this, fulltemplate="", i, link, a; e.uri = "data:application/vnd.ms-excel;base64,"; e.base64 = function (s) { return window.btoa(unescape(encodeuricomponent(s))); }; e.format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }); }; e.ctx = { worksheet: name || "worksheet", table: table }; fulltemplate= e.template.head; if ( $.isarray(table) ) { (i in table) { //fulltemplate += e.template.sheet.head + "{worksheet" + + "}" + e.template.sheet.tail; fulltemplate += e.template.sheet.head + "table" + + "" + e.template.sheet.tail; } } fulltemplate += e.template.mid; if ( $.isarray(table) ) { (i in table) { fulltemplate += e.template.table.head + "{table" + + "}" + e.template.table.tail; } } fulltemplate += e.template.foot; (i in table) { e.ctx["table" + i] = table[i]; } delete e.ctx.table; if (typeof msie !== "undefined" && msie > 0 || !!navigator.useragent.match(/trident.*rv\:11\./)) // if internet explorer { if (typeof blob !== "undefined") { //use blobs if can fulltemplate = [fulltemplate]; //convert array var blob1 = new blob(fulltemplate, { type: "text/html" }); window.navigator.mssaveblob(blob1, getfilename(e.settings) ); } else { //otherwise use iframe , save //requires blank iframe on page called txtarea1 txtarea1.document.open("text/html", "replace"); txtarea1.document.write(e.format(fulltemplate, e.ctx)); txtarea1.document.close(); txtarea1.focus(); sa = txtarea1.document.execcommand("saveas", true, getfilename(e.settings) ); } } else { link = e.uri + e.base64(e.format(fulltemplate, e.ctx)); = document.createelement("a"); a.download = getfilename(e.settings); a.href = link; document.body.appendchild(a); a.click(); document.body.removechild(a); } return true; } }; function getfilename(settings) { return ( settings.filename ? settings.filename : "table2excel") + ".xls"; } $.fn[ pluginname ] = function ( options ) { var e = this; e.each(function() { if ( !$.data( e, "plugin_" + pluginname ) ) { $.data( e, "plugin_" + pluginname, new plugin( this, options ) ); } }); // chain jquery functions return e; }; })( jquery, window, document ); $("#excel").click(function(){ $("#customers").table2excel({ exclude: ".noexl", name: "excel document customers" }); });
check fiddle -> https://jsfiddle.net/dtchh/12997/
Comments
Post a Comment