|
1 | 1 | //table2excel.js |
2 | 2 | ;(function ( $, window, document, undefined ) { |
3 | 3 | var pluginName = "table2excel", |
4 | | - |
5 | 4 | defaults = { |
6 | 5 | exclude: ".noExl", |
7 | | - name: "Table2Excel" |
| 6 | + name: "Table2Excel" |
8 | 7 | }; |
9 | | - |
| 8 | + |
10 | 9 | // The actual plugin constructor |
11 | 10 | function Plugin ( element, options ) { |
12 | 11 | this.element = element; |
13 | 12 | // jQuery has an extend method which merges the contents of two or |
14 | 13 | // more objects, storing the result in the first object. The first object |
15 | 14 | // is generally empty as we don't want to alter the default options for |
16 | 15 | // future instances of the plugin |
17 | | - // |
18 | 16 | this.settings = $.extend( {}, defaults, options ); |
19 | 17 | this._defaults = defaults; |
20 | 18 | this._name = pluginName; |
|
23 | 21 |
|
24 | 22 | Plugin.prototype = { |
25 | 23 | init: function () { |
26 | | - var e = this; |
27 | | - |
28 | | - var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">"; |
| 24 | + var e = this; |
| 25 | + |
| 26 | + var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">"; |
| 27 | + |
29 | 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 | 30 | sheet: { |
31 | 31 | head: "<x:ExcelWorksheet><x:Name>", |
32 | 32 | tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>" |
|
38 | 38 | }, |
39 | 39 | foot: "</body></html>" |
40 | 40 | }; |
41 | | - |
| 41 | + |
42 | 42 | e.tableRows = []; |
43 | | - |
| 43 | + |
44 | 44 | // get contents of table except for exclude |
45 | 45 | $(e.element).each( function(i,o) { |
46 | 46 | var tempRows = ""; |
|
59 | 59 | }); |
60 | 60 | e.tableRows.push(tempRows); |
61 | 61 | }); |
62 | | - |
| 62 | + |
63 | 63 | e.tableToExcel(e.tableRows, e.settings.name, e.settings.sheetName); |
64 | 64 | }, |
65 | | - |
| 65 | + |
66 | 66 | tableToExcel: function (table, name, sheetName) { |
67 | 67 | var e = this, fullTemplate="", i, link, a; |
68 | | - |
| 68 | + |
69 | 69 | e.uri = "data:application/vnd.ms-excel;base64,"; |
70 | 70 | e.base64 = function (s) { |
71 | 71 | return window.btoa(unescape(encodeURIComponent(s))); |
|
75 | 75 | return c[p]; |
76 | 76 | }); |
77 | 77 | }; |
78 | | - |
| 78 | + |
79 | 79 | sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName; |
80 | | - |
| 80 | + |
81 | 81 | e.ctx = { |
82 | 82 | worksheet: name || "Worksheet", |
83 | 83 | table: table, |
84 | 84 | sheetName: sheetName, |
85 | 85 | }; |
86 | | - |
| 86 | + |
87 | 87 | fullTemplate= e.template.head; |
88 | | - |
| 88 | + |
89 | 89 | if ( $.isArray(table) ) { |
90 | 90 | for (i in table) { |
91 | 91 | //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail; |
92 | 92 | fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail; |
93 | 93 | } |
94 | 94 | } |
95 | | - |
| 95 | + |
96 | 96 | fullTemplate += e.template.mid; |
97 | | - |
| 97 | + |
98 | 98 | if ( $.isArray(table) ) { |
99 | 99 | for (i in table) { |
100 | 100 | fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail; |
101 | 101 | } |
102 | 102 | } |
103 | | - |
| 103 | + |
104 | 104 | fullTemplate += e.template.foot; |
105 | | - |
| 105 | + |
106 | 106 | for (i in table) { |
107 | 107 | e.ctx["table" + i] = table[i]; |
108 | 108 | } |
109 | 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 | | - { |
| 110 | + |
| 111 | + var isIE = /*@cc_on!@*/false || !!document.documentMode; // this works with IE10 and IE11 both :) |
| 112 | + if (isIE) { |
114 | 113 | if (typeof Blob !== "undefined") { |
115 | 114 | //use blobs if we can |
116 | 115 | fullTemplate = e.format(fullTemplate, e.ctx); // with this, works with IE |
|
127 | 126 | txtArea1.focus(); |
128 | 127 | sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings) ); |
129 | 128 | } |
130 | | - |
131 | 129 | } else { |
132 | 130 | link = e.uri + e.base64(e.format(fullTemplate, e.ctx)); |
133 | 131 | a = document.createElement("a"); |
134 | 132 | a.download = getFileName(e.settings); |
135 | 133 | a.href = link; |
136 | 134 |
|
137 | 135 | document.body.appendChild(a); |
138 | | - |
139 | 136 | a.click(); |
140 | | - |
141 | 137 | document.body.removeChild(a); |
142 | 138 | } |
143 | 139 |
|
144 | 140 | return true; |
145 | 141 | } |
146 | 142 | }; |
147 | | - |
| 143 | + |
148 | 144 | function getFileName(settings) { |
149 | 145 | return ( settings.filename ? settings.filename : "table2excel" ); |
150 | 146 | } |
|
0 commit comments