-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathjquery.tableToExcel.js
More file actions
54 lines (52 loc) · 2.8 KB
/
jquery.tableToExcel.js
File metadata and controls
54 lines (52 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(function ($) {
$.fn.tblToExcel = function () {
var elm = true;
if (this.length > 1) {
$('body').append('<div id="tbl-tnv-back" style="position: fixed; z-index: 1;padding-top: 100px;left: 0;top: 0;width: 100%;height: 100%;overflow: auto;background-color: rgb(0,0,0);background-color: rgba(0,0,0,0.4);">' +
'<div id="tbl-tnv-excel" style="background-color: #fefefe;margin: auto;' +
'padding: 20px; ' +
'overflow: auto;' +
'border: 1px solid #888;' +
'width: 80%;" > </div>' +
'</div>');
elm = false;
}
$('#tbl-tnv-back').click(function () {
$(this).remove();
$('#tbl-tnv-anch').remove();
});
var tableToExcel = (function () {
var i = 0;
var uri = 'data:application/vnd.ms-excel;base64,',
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 charset="utf-8"/><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, 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
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
if (elm) {
window.location.href = uri + base64(format(template, ctx));
} else {
i++;
var xl = uri + base64(format(template, ctx));
$('#tbl-tnv-excel').append('<a id="tbl-tnv-anch" style="background-color: #4CAF50;border: none;\n' +
'color: white;' +
'padding: 15px 32px;' +
'text-align: center;' +
'text-decoration: none;' +
'display: inline-block; margin: 1px;' +
'font-size: 16px;" href='+xl+' download>Download Excel-'+i+' </a>');
}
}
})();
return this.each(function () {
tableToExcel(this, 'W3C Example Table');
});
}
}(jQuery));