|
8 | 8 | */
|
9 | 9 | //table2excel.js
|
10 | 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><!--[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]); |
| 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 | + var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">"; |
| 37 | + e.template = { |
| 38 | + 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\">" + utf8Heading + "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>", |
| 39 | + sheet: { |
| 40 | + head: "<x:ExcelWorksheet><x:Name>", |
| 41 | + tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>" |
| 42 | + }, |
| 43 | + mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>", |
| 44 | + table: { |
| 45 | + head: "<table>", |
| 46 | + tail: "</table>" |
| 47 | + }, |
| 48 | + foot: "</body></html>" |
| 49 | + }; |
| 50 | + |
| 51 | + e.tableRows = []; |
| 52 | + |
| 53 | + // get contents of table except for exclude |
| 54 | + $(e.element).each( function(i,o) { |
| 55 | + var tempRows = ""; |
| 56 | + $(o).find("tr").not(e.settings.exclude).each(function (i,o) { |
| 57 | + tempRows += "<tr>" + $(o).html() + "</tr>"; |
| 58 | + }); |
| 59 | + e.tableRows.push(tempRows); |
| 60 | + }); |
| 61 | + |
| 62 | + e.tableToExcel(e.tableRows, e.settings.name, e.settings.sheetName); |
| 63 | + }, |
| 64 | + |
| 65 | + tableToExcel: function (table, name, sheetName) { |
| 66 | + var e = this, fullTemplate="", i, link, a; |
| 67 | + |
| 68 | + e.uri = "data:application/vnd.ms-excel;base64,"; |
| 69 | + e.base64 = function (s) { |
| 70 | + return window.btoa(unescape(encodeURIComponent(s))); |
| 71 | + }; |
| 72 | + e.format = function (s, c) { |
| 73 | + return s.replace(/{(\w+)}/g, function (m, p) { |
| 74 | + return c[p]; |
| 75 | + }); |
| 76 | + }; |
| 77 | + |
| 78 | + sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName; |
| 79 | + |
| 80 | + e.ctx = { |
| 81 | + worksheet: name || "Worksheet", |
| 82 | + table: table, |
| 83 | + sheetName: sheetName, |
| 84 | + }; |
| 85 | + |
| 86 | + fullTemplate= e.template.head; |
| 87 | + |
| 88 | + if ( $.isArray(table) ) { |
| 89 | + for (i in table) { |
| 90 | + //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail; |
| 91 | + fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail; |
| 92 | + } |
64 | 93 | }
|
65 | 94 |
|
66 |
| - // exclude link tags |
67 |
| - if(e.settings.exclude_links) { |
68 |
| - e.tableRows[0] = exclude_links(e.tableRows[0]); |
| 95 | + fullTemplate += e.template.mid; |
| 96 | + |
| 97 | + if ( $.isArray(table) ) { |
| 98 | + for (i in table) { |
| 99 | + fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail; |
| 100 | + } |
69 | 101 | }
|
70 | 102 |
|
71 |
| - // exclude input tags |
72 |
| - if(e.settings.exclude_inputs) { |
73 |
| - e.tableRows[0] = exclude_inputs(e.tableRows[0]) |
| 103 | + fullTemplate += e.template.foot; |
| 104 | + |
| 105 | + for (i in table) { |
| 106 | + e.ctx["table" + i] = table[i]; |
| 107 | + } |
| 108 | + delete e.ctx.table; |
| 109 | + |
| 110 | + if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer |
| 111 | + { |
| 112 | + if (typeof Blob !== "undefined") { |
| 113 | + //use blobs if we can |
| 114 | + fullTemplate = [fullTemplate]; |
| 115 | + //convert to array |
| 116 | + var blob1 = new Blob(fullTemplate, { type: "text/html" }); |
| 117 | + window.navigator.msSaveBlob(blob1, getFileName(e.settings) ); |
| 118 | + } else { |
| 119 | + //otherwise use the iframe and save |
| 120 | + //requires a blank iframe on page called txtArea1 |
| 121 | + txtArea1.document.open("text/html", "replace"); |
| 122 | + txtArea1.document.write(e.format(fullTemplate, e.ctx)); |
| 123 | + txtArea1.document.close(); |
| 124 | + txtArea1.focus(); |
| 125 | + sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings) ); |
| 126 | + } |
| 127 | + |
| 128 | + } else { |
| 129 | + link = e.uri + e.base64(e.format(fullTemplate, e.ctx)); |
| 130 | + a = document.createElement("a"); |
| 131 | + a.download = getFileName(e.settings); |
| 132 | + a.href = link; |
| 133 | + |
| 134 | + document.body.appendChild(a); |
| 135 | + |
| 136 | + a.click(); |
| 137 | + |
| 138 | + document.body.removeChild(a); |
74 | 139 | }
|
75 | 140 |
|
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 |
| - }; |
| 141 | + return true; |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + function getFileName(settings) { |
| 146 | + return ( settings.filename ? settings.filename : "table2excel" ) + |
| 147 | + ( settings.fileext ? settings.fileext : ".xlsx" ); |
| 148 | + } |
| 149 | + |
| 150 | + $.fn[ pluginName ] = function ( options ) { |
| 151 | + var e = this; |
| 152 | + e.each(function() { |
| 153 | + if ( !$.data( e, "plugin_" + pluginName ) ) { |
| 154 | + $.data( e, "plugin_" + pluginName, new Plugin( this, options ) ); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + // chain jQuery functions |
| 159 | + return e; |
| 160 | + }; |
182 | 161 |
|
183 | 162 | })( jQuery, window, document );
|
0 commit comments