Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit ec62d58

Browse files
committed
Support unicode and file extension feature.
1 parent 3bbc8ff commit ec62d58

File tree

4 files changed

+151
-169
lines changed

4 files changed

+151
-169
lines changed

demo/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
exclude: ".noExl",
4141
name: "Excel Document Name",
4242
filename: "myFileName",
43+
fileext: ".xls",
4344
exclude_img: true,
4445
exclude_links: true,
4546
exclude_inputs: true

dist/jquery.table2excel.js

+144-165
Original file line numberDiff line numberDiff line change
@@ -8,176 +8,155 @@
88
*/
99
//table2excel.js
1010
;(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+
}
6493
}
6594

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+
}
69101
}
70102

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);
74139
}
75140

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+
};
182161

183162
})( jQuery, window, document );

dist/jquery.table2excel.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.table2excel.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
init: function () {
2626
var e = this;
2727

28+
var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">";
2829
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\"><meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",
30+
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>",
3031
sheet: {
3132
head: "<x:ExcelWorksheet><x:Name>",
3233
tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
@@ -66,7 +67,7 @@
6667
});
6768
};
6869

69-
sheetName = typeof sheetName == "undefined" ? "Sheet" : sheetName;
70+
sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName;
7071

7172
e.ctx = {
7273
worksheet: name || "Worksheet",
@@ -134,7 +135,8 @@
134135
};
135136

136137
function getFileName(settings) {
137-
return ( settings.filename ? settings.filename : "table2excel") + ".xls";
138+
return ( settings.filename ? settings.filename : "table2excel" ) +
139+
( settings.fileext ? settings.fileext : ".xlsx" );
138140
}
139141

140142
$.fn[ pluginName ] = function ( options ) {

0 commit comments

Comments
 (0)