|
1 | 1 | //table2excel.js
|
2 | 2 | ;(function ( $, window, document, undefined ) {
|
3 |
| - var pluginName = "table2excel", |
4 |
| - defaults = { |
5 |
| - exclude: ".noExl", |
6 |
| - name: "Table2Excel" |
7 |
| - }; |
8 |
| - |
9 |
| - // The actual plugin constructor |
10 |
| - function Plugin ( element, options ) { |
11 |
| - this.element = element; |
12 |
| - // jQuery has an extend method which merges the contents of two or |
13 |
| - // more objects, storing the result in the first object. The first object |
14 |
| - // is generally empty as we don't want to alter the default options for |
15 |
| - // future instances of the plugin |
16 |
| - this.settings = $.extend( {}, defaults, options ); |
17 |
| - this._defaults = defaults; |
18 |
| - this._name = pluginName; |
19 |
| - this.init(); |
20 |
| - } |
| 3 | + var pluginName = "table2excel", |
| 4 | + defaults = { |
| 5 | + exclude: ".noExl", |
| 6 | + name: "Table2Excel" |
| 7 | + }; |
| 8 | + |
| 9 | + // The actual plugin constructor |
| 10 | + function Plugin ( element, options ) { |
| 11 | + this.element = element; |
| 12 | + // jQuery has an extend method which merges the contents of two or |
| 13 | + // more objects, storing the result in the first object. The first object |
| 14 | + // is generally empty as we don't want to alter the default options for |
| 15 | + // future instances of the plugin |
| 16 | + this.settings = $.extend( {}, defaults, options ); |
| 17 | + this._defaults = defaults; |
| 18 | + this._name = pluginName; |
| 19 | + this.init(); |
| 20 | + } |
21 | 21 |
|
22 |
| - Plugin.prototype = { |
23 |
| - init: function () { |
24 |
| - var e = this; |
25 |
| - |
26 |
| - var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">"; |
27 |
| - |
28 |
| - e.template = { |
29 |
| - 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>", |
30 |
| - sheet: { |
31 |
| - head: "<x:ExcelWorksheet><x:Name>", |
32 |
| - tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>" |
33 |
| - }, |
34 |
| - mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>", |
35 |
| - table: { |
36 |
| - head: "<table>", |
37 |
| - tail: "</table>" |
38 |
| - }, |
39 |
| - foot: "</body></html>" |
40 |
| - }; |
41 |
| - |
42 |
| - e.tableRows = []; |
43 |
| - |
44 |
| - // get contents of table except for exclude |
45 |
| - $(e.element).each( function(i,o) { |
46 |
| - var tempRows = ""; |
47 |
| - $(o).find("tr").not(e.settings.exclude).each(function (i,o) { |
48 |
| - tempRows += "<tr>"; |
49 |
| - $(o).find("td").not(e.settings.exclude).each(function (i,q) { |
50 |
| - var flag = $(q).find(e.settings.exclude) // does this <td> have something with an exclude class |
51 |
| - if(flag.length >= 1) { |
52 |
| - tempRows += "<td> </td>" // exclude it!! |
53 |
| - } else { |
54 |
| - tempRows += "<td>" + $(q).html() + "</td>" |
55 |
| - } |
56 |
| - }) |
57 |
| - |
58 |
| - tempRows += "</tr>"; |
59 |
| - }); |
60 |
| - e.tableRows.push(tempRows); |
61 |
| - }); |
62 |
| - |
63 |
| - e.tableToExcel(e.tableRows, e.settings.name, e.settings.sheetName); |
64 |
| - }, |
65 |
| - |
66 |
| - tableToExcel: function (table, name, sheetName) { |
67 |
| - var e = this, fullTemplate="", i, link, a; |
68 |
| - |
69 |
| - e.uri = "data:application/vnd.ms-excel;base64,"; |
70 |
| - e.base64 = function (s) { |
71 |
| - return window.btoa(unescape(encodeURIComponent(s))); |
72 |
| - }; |
73 |
| - e.format = function (s, c) { |
74 |
| - return s.replace(/{(\w+)}/g, function (m, p) { |
75 |
| - return c[p]; |
76 |
| - }); |
77 |
| - }; |
78 |
| - |
79 |
| - sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName; |
80 |
| - |
81 |
| - e.ctx = { |
82 |
| - worksheet: name || "Worksheet", |
83 |
| - table: table, |
84 |
| - sheetName: sheetName, |
85 |
| - }; |
86 |
| - |
87 |
| - fullTemplate= e.template.head; |
88 |
| - |
89 |
| - if ( $.isArray(table) ) { |
90 |
| - for (i in table) { |
91 |
| - //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail; |
92 |
| - fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail; |
93 |
| - } |
94 |
| - } |
95 |
| - |
96 |
| - fullTemplate += e.template.mid; |
97 |
| - |
98 |
| - if ( $.isArray(table) ) { |
99 |
| - for (i in table) { |
100 |
| - fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail; |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 |
| - fullTemplate += e.template.foot; |
105 |
| - |
106 |
| - for (i in table) { |
107 |
| - e.ctx["table" + i] = table[i]; |
108 |
| - } |
109 |
| - delete e.ctx.table; |
110 |
| - |
111 |
| - var isIE = /*@cc_on!@*/false || !!document.documentMode; // this works with IE10 and IE11 both :) |
112 |
| - if (isIE) { |
113 |
| - if (typeof Blob !== "undefined") { |
114 |
| - //use blobs if we can |
115 |
| - fullTemplate = e.format(fullTemplate, e.ctx); // with this, works with IE |
116 |
| - fullTemplate = [fullTemplate]; |
117 |
| - //convert to array |
118 |
| - var blob1 = new Blob(fullTemplate, { type: "text/html" }); |
119 |
| - window.navigator.msSaveBlob(blob1, getFileName(e.settings) ); |
120 |
| - } else { |
121 |
| - //otherwise use the iframe and save |
122 |
| - //requires a blank iframe on page called txtArea1 |
123 |
| - txtArea1.document.open("text/html", "replace"); |
124 |
| - txtArea1.document.write(e.format(fullTemplate, e.ctx)); |
125 |
| - txtArea1.document.close(); |
126 |
| - txtArea1.focus(); |
127 |
| - sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings) ); |
128 |
| - } |
129 |
| - } else { |
130 |
| - link = e.uri + e.base64(e.format(fullTemplate, e.ctx)); |
131 |
| - a = document.createElement("a"); |
132 |
| - a.download = getFileName(e.settings); |
133 |
| - a.href = link; |
| 22 | +Plugin.prototype = { |
| 23 | + init: function () { |
| 24 | + var e = this; |
| 25 | + var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">"; |
| 26 | + e.template = { |
| 27 | + 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>", |
| 28 | + sheet: { |
| 29 | + head: "<x:ExcelWorksheet><x:Name>", |
| 30 | + tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>" |
| 31 | + }, |
| 32 | + mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>", |
| 33 | + table: { |
| 34 | + head: "<table>", |
| 35 | + tail: "</table>" |
| 36 | + }, |
| 37 | + foot: "</body></html>" |
| 38 | + }; |
| 39 | + |
| 40 | + e.tableRows = []; |
| 41 | + |
| 42 | + // get contents of table except for exclude |
| 43 | + $(e.element).each( function(i,o) { |
| 44 | + var tempRows = ""; |
| 45 | + $(o).find("tr").not(e.settings.exclude).each(function (i,o) { |
| 46 | + tempRows += "<tr>"; |
| 47 | + $(o).find("td").not(e.settings.exclude).each(function (i,q) { |
| 48 | + var flag = $(q).find(e.settings.exclude) // does this <td> have something with an exclude class |
| 49 | + if(flag.length >= 1) { |
| 50 | + tempRows += "<td> </td>" // exclude it!! |
| 51 | + } else { |
| 52 | + tempRows += "<td>" + $(q).html() + "</td>" |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + tempRows += "</tr>"; |
| 57 | + }); |
| 58 | + e.tableRows.push(tempRows); |
| 59 | + }); |
| 60 | + |
| 61 | + e.tableToExcel(e.tableRows, e.settings.name, e.settings.sheetName); |
| 62 | + }, |
| 63 | + |
| 64 | + tableToExcel: function (table, name, sheetName) { |
| 65 | + var e = this, fullTemplate="", i, link, a; |
| 66 | + |
| 67 | + e.uri = "data:application/vnd.ms-excel;base64,"; |
| 68 | + e.base64 = function (s) { |
| 69 | + return window.btoa(unescape(encodeURIComponent(s))); |
| 70 | + }; |
| 71 | + e.format = function (s, c) { |
| 72 | + return s.replace(/{(\w+)}/g, function (m, p) { |
| 73 | + return c[p]; |
| 74 | + }); |
| 75 | + }; |
| 76 | + |
| 77 | + sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName; |
| 78 | + |
| 79 | + e.ctx = { |
| 80 | + worksheet: name || "Worksheet", |
| 81 | + table: table, |
| 82 | + sheetName: sheetName, |
| 83 | + }; |
| 84 | + |
| 85 | + fullTemplate= e.template.head; |
| 86 | + |
| 87 | + if ( $.isArray(table) ) { |
| 88 | + for (i in table) { |
| 89 | + //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail; |
| 90 | + fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + fullTemplate += e.template.mid; |
| 95 | + |
| 96 | + if ( $.isArray(table) ) { |
| 97 | + for (i in table) { |
| 98 | + fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + fullTemplate += e.template.foot; |
| 103 | + |
| 104 | + for (i in table) { |
| 105 | + e.ctx["table" + i] = table[i]; |
| 106 | + } |
| 107 | + delete e.ctx.table; |
| 108 | + |
| 109 | + var isIE = /*@cc_on!@*/false || !!document.documentMode; // this works with IE10 and IE11 both :) |
| 110 | + if (isIE) { |
| 111 | + if (typeof Blob !== "undefined") { |
| 112 | + //use blobs if we can |
| 113 | + fullTemplate = e.format(fullTemplate, e.ctx); // with this, works with IE |
| 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 | + } else { |
| 128 | + link = e.uri + e.base64(e.format(fullTemplate, e.ctx)); |
| 129 | + a = document.createElement("a"); |
| 130 | + a.download = getFileName(e.settings); |
| 131 | + a.href = link; |
134 | 132 |
|
135 |
| - document.body.appendChild(a); |
136 |
| - a.click(); |
137 |
| - document.body.removeChild(a); |
138 |
| - } |
| 133 | + document.body.appendChild(a); |
| 134 | + a.click(); |
| 135 | + document.body.removeChild(a); |
| 136 | + } |
139 | 137 |
|
140 |
| - return true; |
141 |
| - } |
142 |
| - }; |
143 |
| - |
144 |
| - function getFileName(settings) { |
145 |
| - return ( settings.filename ? settings.filename : "table2excel" ); |
146 |
| - } |
| 138 | + return true; |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + function getFileName(settings) { |
| 143 | + return ( settings.filename ? settings.filename : "table2excel" ); |
| 144 | + } |
147 | 145 |
|
148 |
| - $.fn[ pluginName ] = function ( options ) { |
149 |
| - var e = this; |
150 |
| - e.each(function() { |
151 |
| - if ( !$.data( e, "plugin_" + pluginName ) ) { |
152 |
| - $.data( e, "plugin_" + pluginName, new Plugin( this, options ) ); |
153 |
| - } |
154 |
| - }); |
| 146 | + $.fn[ pluginName ] = function ( options ) { |
| 147 | + var e = this; |
| 148 | + e.each(function() { |
| 149 | + if ( !$.data( e, "plugin_" + pluginName ) ) { |
| 150 | + $.data( e, "plugin_" + pluginName, new Plugin( this, options ) ); |
| 151 | + } |
| 152 | + }); |
155 | 153 |
|
156 |
| - // chain jQuery functions |
157 |
| - return e; |
158 |
| - }; |
| 154 | + // chain jQuery functions |
| 155 | + return e; |
| 156 | + }; |
159 | 157 |
|
160 | 158 | })( jQuery, window, document );
|
0 commit comments