|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+/*
|
|
|
2
|
+ * jQuery table2excel - v1.0.2
|
|
|
3
|
+ * jQuery plugin to export an .xls file in browser from an HTML table
|
|
|
4
|
+ * https://github.com/rainabba/jquery-table2excel
|
|
|
5
|
+ *
|
|
|
6
|
+ * Made by rainabba
|
|
|
7
|
+ * Under MIT License
|
|
|
8
|
+ */
|
|
|
9
|
+//table2excel.js
|
|
|
10
|
+;(function ( $, window, document, undefined ) {
|
|
|
11
|
+ var pluginName = "table2excel",
|
|
|
12
|
+
|
|
|
13
|
+ defaults = {
|
|
|
14
|
+ exclude: ".noExl",
|
|
|
15
|
+ name: "Table2Excel"
|
|
|
16
|
+ };
|
|
|
17
|
+
|
|
|
18
|
+ // The actual plugin constructor
|
|
|
19
|
+ function Plugin ( element, options ) {
|
|
|
20
|
+ this.element = element;
|
|
|
21
|
+ // jQuery has an extend method which merges the contents of two or
|
|
|
22
|
+ // more objects, storing the result in the first object. The first object
|
|
|
23
|
+ // is generally empty as we don't want to alter the default options for
|
|
|
24
|
+ // future instances of the plugin
|
|
|
25
|
+ //
|
|
|
26
|
+ this.settings = $.extend( {}, defaults, options );
|
|
|
27
|
+ this._defaults = defaults;
|
|
|
28
|
+ this._name = pluginName;
|
|
|
29
|
+ this.init();
|
|
|
30
|
+ }
|
|
|
31
|
+
|
|
|
32
|
+ Plugin.prototype = {
|
|
|
33
|
+ init: function () {
|
|
|
34
|
+ var e = this;
|
|
|
35
|
+
|
|
|
36
|
+ e.template = {
|
|
|
37
|
+ 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><meta charset=\"UTF-8\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",
|
|
|
38
|
+ sheet: {
|
|
|
39
|
+ head: "<x:ExcelWorksheet><x:Name>",
|
|
|
40
|
+ tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
|
|
|
41
|
+ },
|
|
|
42
|
+ mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",
|
|
|
43
|
+ table: {
|
|
|
44
|
+ head: "<table>",
|
|
|
45
|
+ tail: "</table>"
|
|
|
46
|
+ },
|
|
|
47
|
+ foot: "</body></html>"
|
|
|
48
|
+ };
|
|
|
49
|
+
|
|
|
50
|
+ e.tableRows = [];
|
|
|
51
|
+
|
|
|
52
|
+ // get contents of table except for exclude
|
|
|
53
|
+ $(e.element).each( function(i,o) {
|
|
|
54
|
+ var tempRows = "";
|
|
|
55
|
+ $(o).find("tr").not(e.settings.exclude).each(function (i,o) {
|
|
|
56
|
+ tempRows += "<tr>" + $(o).html() + "</tr>";
|
|
|
57
|
+ });
|
|
|
58
|
+ e.tableRows.push(tempRows);
|
|
|
59
|
+ });
|
|
|
60
|
+
|
|
|
61
|
+ // exclude img tags
|
|
|
62
|
+ if(e.settings.exclude_img) {
|
|
|
63
|
+ e.tableRows[0] = exclude_img(e.tableRows[0]);
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ // exclude link tags
|
|
|
67
|
+ if(e.settings.exclude_links) {
|
|
|
68
|
+ e.tableRows[0] = exclude_links(e.tableRows[0]);
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ // exclude input tags
|
|
|
72
|
+ if(e.settings.exclude_inputs) {
|
|
|
73
|
+ e.tableRows[0] = exclude_inputs(e.tableRows[0])
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ e.tableToExcel(e.tableRows, e.settings.name);
|
|
|
77
|
+ },
|
|
|
78
|
+
|
|
|
79
|
+ tableToExcel: function (table, name) {
|
|
|
80
|
+ var e = this, fullTemplate="", i, link, a;
|
|
|
81
|
+
|
|
|
82
|
+ e.uri = "data:application/vnd.ms-excel;base64,";
|
|
|
83
|
+ e.base64 = function (s) {
|
|
|
84
|
+ return window.btoa(unescape(encodeURIComponent(s)));
|
|
|
85
|
+ };
|
|
|
86
|
+ e.format = function (s, c) {
|
|
|
87
|
+ return s.replace(/{(\w+)}/g, function (m, p) {
|
|
|
88
|
+ return c[p];
|
|
|
89
|
+ });
|
|
|
90
|
+ };
|
|
|
91
|
+ e.ctx = {
|
|
|
92
|
+ worksheet: name || "Worksheet",
|
|
|
93
|
+ table: table
|
|
|
94
|
+ };
|
|
|
95
|
+
|
|
|
96
|
+ fullTemplate= e.template.head;
|
|
|
97
|
+
|
|
|
98
|
+ if ( $.isArray(table) ) {
|
|
|
99
|
+ for (i in table) {
|
|
|
100
|
+ //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;
|
|
|
101
|
+ fullTemplate += e.template.sheet.head + "Table" + i + "" + e.template.sheet.tail;
|
|
|
102
|
+ }
|
|
|
103
|
+ }
|
|
|
104
|
+
|
|
|
105
|
+ fullTemplate += e.template.mid;
|
|
|
106
|
+
|
|
|
107
|
+ if ( $.isArray(table) ) {
|
|
|
108
|
+ for (i in table) {
|
|
|
109
|
+ fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
|
|
|
110
|
+ }
|
|
|
111
|
+ }
|
|
|
112
|
+
|
|
|
113
|
+ fullTemplate += e.template.foot;
|
|
|
114
|
+
|
|
|
115
|
+ for (i in table) {
|
|
|
116
|
+ e.ctx["table" + i] = table[i];
|
|
|
117
|
+ }
|
|
|
118
|
+ delete e.ctx.table;
|
|
|
119
|
+
|
|
|
120
|
+
|
|
|
121
|
+ if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
|
|
|
122
|
+ {
|
|
|
123
|
+ if (typeof Blob !== "undefined") {
|
|
|
124
|
+ //use blobs if we can
|
|
|
125
|
+ fullTemplate = [fullTemplate];
|
|
|
126
|
+ //convert to array
|
|
|
127
|
+ var blob1 = new Blob(fullTemplate, { type: "text/html" });
|
|
|
128
|
+ window.navigator.msSaveBlob(blob1, getFileName(e.settings) );
|
|
|
129
|
+ } else {
|
|
|
130
|
+ //otherwise use the iframe and save
|
|
|
131
|
+ //requires a blank iframe on page called txtArea1
|
|
|
132
|
+ txtArea1.document.open("text/html", "replace");
|
|
|
133
|
+ txtArea1.document.write(fullTemplate);
|
|
|
134
|
+ txtArea1.document.close();
|
|
|
135
|
+ txtArea1.focus();
|
|
|
136
|
+ sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings) );
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ } else {
|
|
|
140
|
+ link = e.uri + e.base64(e.format(fullTemplate, e.ctx));
|
|
|
141
|
+ a = document.createElement("a");
|
|
|
142
|
+ a.download = getFileName(e.settings);
|
|
|
143
|
+ a.href = link;
|
|
|
144
|
+ a.click();
|
|
|
145
|
+ }
|
|
|
146
|
+
|
|
|
147
|
+ return true;
|
|
|
148
|
+
|
|
|
149
|
+ }
|
|
|
150
|
+ };
|
|
|
151
|
+
|
|
|
152
|
+ function getFileName(settings) {
|
|
|
153
|
+ return ( settings.filename ? settings.filename : "table2excel") + ".xlsx";
|
|
|
154
|
+ }
|
|
|
155
|
+
|
|
|
156
|
+ // Removes all img tags
|
|
|
157
|
+ function exclude_img(string) {
|
|
|
158
|
+ return string.replace(/<img[^>]*>/gi,"");
|
|
|
159
|
+ }
|
|
|
160
|
+
|
|
|
161
|
+ // Removes all link tags
|
|
|
162
|
+ function exclude_links(string) {
|
|
|
163
|
+ return string.replace(/<A[^>]*>|<\/A>/g, "");
|
|
|
164
|
+ }
|
|
|
165
|
+
|
|
|
166
|
+ // Removes input params
|
|
|
167
|
+ function exclude_inputs(string) {
|
|
|
168
|
+ return string.replace(/<input[^>]*>|<\/input>/gi, "");
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ $.fn[ pluginName ] = function ( options ) {
|
|
|
172
|
+ var e = this;
|
|
|
173
|
+ e.each(function() {
|
|
|
174
|
+ if ( !$.data( e, "plugin_" + pluginName ) ) {
|
|
|
175
|
+ $.data( e, "plugin_" + pluginName, new Plugin( this, options ) );
|
|
|
176
|
+ }
|
|
|
177
|
+ });
|
|
|
178
|
+
|
|
|
179
|
+ // chain jQuery functions
|
|
|
180
|
+ return e;
|
|
|
181
|
+ };
|
|
|
182
|
+
|
|
|
183
|
+})( jQuery, window, document );
|