Last active
March 3, 2016 17:08
-
-
Save azborgonovo/6234968a68ca5768a353 to your computer and use it in GitHub Desktop.
Crossbrowser HTML to Excel (adapted table2excel)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//table2excel.js | |
; (function ($, window, document, undefined) { | |
var pluginName = "table2excel", | |
defaults = { | |
exclude: ".noExl", | |
name: "Table2Excel" | |
}; | |
// The actual plugin constructor | |
function Plugin(element, options) { | |
this.element = element; | |
// jQuery has an extend method which merges the contents of two or | |
// more objects, storing the result in the first object. The first object | |
// is generally empty as we don't want to alter the default options for | |
// future instances of the plugin | |
this.settings = $.extend({}, defaults, options); | |
this._defaults = defaults; | |
this._name = pluginName; | |
this.init(); | |
} | |
Plugin.prototype = { | |
init: function () { | |
var e = this; | |
e.template = "<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\"><meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\"><head><!--[if gte mso 9]><xml>"; | |
e.template += "<x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>"; | |
e.template += "<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>"; | |
e.tableRows = ""; | |
// get contents of table except for exclude | |
$(e.element).find("tr").not(this.settings.exclude).each(function (i, o) { | |
e.tableRows += "<tr>" + $(o).html() + "</tr>"; | |
}); | |
this.tableToExcel(this.tableRows, this.settings.name); | |
}, | |
tableToExcel: function (table, name) { | |
var e = this; | |
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 | |
}; | |
if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer | |
{ | |
if (typeof Blob !== "undefined") { | |
//use blobs if we can | |
var fullTemplate = [e.format(e.template, e.ctx)]; | |
//convert to array | |
var blob1 = new Blob(fullTemplate, { type: "text/html" }); | |
window.navigator.msSaveBlob(blob1, name + ".xls"); | |
} else { | |
//otherwise use the iframe and save | |
var iframe = document.createElement('iframe'); | |
document.body.appendChild(iframe); | |
iframe.contentWindow.document.open(); | |
iframe.contentWindow.document.write(fullTemplate); | |
iframe.contentWindow.document.close(); | |
// iframe.focus(); // Crashes IE | |
sa = iframe.contentWindow.document.execCommand("SaveAs", true, name + ".xls"); | |
} | |
} else { | |
window.location.href = e.uri + e.base64(e.format(e.template, e.ctx)); | |
} | |
} | |
}; | |
$.fn[pluginName] = function (options) { | |
this.each(function () { | |
if (!$.data(this, "plugin_" + pluginName)) { | |
$.data(this, "plugin_" + pluginName, new Plugin(this, options)); | |
} | |
}); | |
// chain jQuery functions | |
return this; | |
}; | |
})(jQuery, window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment