怎么让tableExport支持中文导出
答案:1 悬赏:0 手机版
解决时间 2021-02-02 20:44
- 提问者网友:一抹荒凉废墟
- 2021-02-02 16:05
怎么让tableExport支持中文导出
最佳答案
- 五星知识达人网友:掌灯师
- 2021-02-02 17:12
一、mybase64.js
使用方法
//=======编码,type:0 可以省略data = $.base64({data:"需要编码成base64的字符串",type:0});
//=======解码
data = $.base64({data:"已经编码过后的base64",type:1,unicode:false});
//=======让解码后中文不出现乱码的方法
data = $.base64({data:"已经编码过后的base64",type:1,unicode:true});
//=或
data = $.base64({data:"已经编码过后的base64",type:1});
(function ($) {
$.base64 = function (options) {
var defaults = {
data: "",
type: 0,
unicode: true
};
var opts = $.extend(defaults, options);
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
UnicodeChr = function () {
UnicodeToAnsi = function (chrCode) {
var chrHex = chrCode.toString(16);
chrHex = "000" + chrHex.toUpperCase();
chrHex = chrHex.substr(chrHex.length - 4);
var i = UnicodeChr().indexOf(chrHex);
if (i != -1) {
chrHex = AnsicodeChr().substr(i, 4);
}
return parseInt(chrHex, 16)
}
AnsiToUnicode = function (chrCode) {
var chrHex = chrCode.toString(16);
chrHex = "000" + chrHex.toUpperCase();
chrHex = chrHex.substr(chrHex.length - 4);
var i = AnsicodeChr().indexOf(chrHex);
if (i != -1) {
chrHex = UnicodeChr().substr(i, 4);
}
return parseInt(chrHex, 16)
}
strUnicode2Ansi = function (asContents) {
var len1 = asContents.length;
var temp = "";
for (var i = 0; i < len1; i++) {
var varasc = asContents.charCodeAt(i);
if (varasc < 0)
varasc += 65536;
if (varasc > 127)
varasc = UnicodeToAnsi(varasc);
if (varasc > 255) {
var varlow = varasc & 65280;
varlow = varlow >> 8;
var varhigh = varasc & 255;
temp += String.fromCharCode(varlow) + String.fromCharCode(varhigh);
}
else {
temp += String.fromCharCode(varasc);
}
}
return temp;
}
strAnsi2Unicode = function (asContents) {
var len1 = asContents.length;
var temp = "";
var chrcode;
for (var i = 0; i < len1; i++) {
var varasc = asContents.charCodeAt(i);
if (varasc > 127) {
chrcode = AnsiToUnicode((varasc << 8) + asContents.charCodeAt(++i));
}
else {
chrcode = varasc;
}
temp += String.fromCharCode(chrcode);
}
return temp;
}
encode64 = function (input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
keyStr.charAt(enc1) +
keyStr.charAt(enc2) +
keyStr.charAt(enc3) +
keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
decode64 = function (input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
if (input.length % 4 != 0) {
return "";
}
var base64test = /[^A-Za-z0-9/+///=]/g;
if (base64test.exec(input)) {
return "";
}
do {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output += String.fromCharCode(chr2);
}
if (enc4 != 64) {
output += String.fromCharCode(chr3);
}
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
if (opts.data == "") {
return false;
} else {
if (opts.type == 0) {
if (opts.unicode == true) {
return encode64(strUnicode2Ansi(opts.data));
} else {
return encode64(opts.data);
}
} else {
if (opts.unicode == true) {
return strAnsi2Unicode(decode64(opts.data));
} else {
return decode64(opts.data);
}
}
}
}
})(jQuery);
二、tableExport.js:
(function($){
$.fn.extend({
tableExport: function(options) {
var defaults = {
separator: ',',
ignoreColumn: [],
tableName:'yourTableName',
type:'csv',
pdfFontSize:14,
pdfLeftMargin:20,
escape:'true',
htmlContent:'false',
consoleLog:'false'
};
var options = $.extend(defaults, options);
var el = this;
if(defaults.type == 'csv' || defaults.type == 'txt'){
// Header
var tdData ="";
$(el).find('thead').find('tr').each(function() {
tdData += "\n";
$(this).filter(':visible').find('th').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
tdData += '"' + parseString($(this)) + '"' + defaults.separator;
}
}
});
tdData = $.trim(tdData);
tdData = $.trim(tdData).substring(0, tdData.length -1);
});
// Row vs Column
$(el).find('tbody').find('tr').each(function() {
tdData += "\n";
$(this).filter(':visible').find('td').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
tdData += '"'+ parseString($(this)) + '"'+ defaults.separator;
}
}
});
//tdData = $.trim(tdData);
tdData = $.trim(tdData).substring(0, tdData.length -1);
});
//output
if(defaults.consoleLog == 'true'){
console.log(tdData);
}
var base64data = "base64," + $.base64.encode(tdData);
window.open('data:application/'+defaults.type+';filename=exportData;' + base64data);
}else if(defaults.type == 'sql'){
// Header
var tdData ="INSERT INTO `"+defaults.tableName+"` (";
$(el).find('thead').find('tr').each(function() {
$(this).filter(':visible').find('th').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
tdData += '`' + parseString($(this)) + '`,' ;
}
}
});
tdData = $.trim(tdData);
tdData = $.trim(tdData).substring(0, tdData.length -1);
});
tdData += ") VALUES ";
// Row vs Column
$(el).find('tbody').find('tr').each(function() {
tdData += "(";
$(this).filter(':visible').find('td').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
tdData += '"'+ parseString($(this)) + '",';
}
}
});
三、引用和导出
1.注意引入jquery.min.js,tableExport.js,mybase64.js;
2.导出 $('#ID').tableExport({ type: 'excel', escape: 'false' });
使用方法
//=======编码,type:0 可以省略data = $.base64({data:"需要编码成base64的字符串",type:0});
//=======解码
data = $.base64({data:"已经编码过后的base64",type:1,unicode:false});
//=======让解码后中文不出现乱码的方法
data = $.base64({data:"已经编码过后的base64",type:1,unicode:true});
//=或
data = $.base64({data:"已经编码过后的base64",type:1});
(function ($) {
$.base64 = function (options) {
var defaults = {
data: "",
type: 0,
unicode: true
};
var opts = $.extend(defaults, options);
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
UnicodeChr = function () {
UnicodeToAnsi = function (chrCode) {
var chrHex = chrCode.toString(16);
chrHex = "000" + chrHex.toUpperCase();
chrHex = chrHex.substr(chrHex.length - 4);
var i = UnicodeChr().indexOf(chrHex);
if (i != -1) {
chrHex = AnsicodeChr().substr(i, 4);
}
return parseInt(chrHex, 16)
}
AnsiToUnicode = function (chrCode) {
var chrHex = chrCode.toString(16);
chrHex = "000" + chrHex.toUpperCase();
chrHex = chrHex.substr(chrHex.length - 4);
var i = AnsicodeChr().indexOf(chrHex);
if (i != -1) {
chrHex = UnicodeChr().substr(i, 4);
}
return parseInt(chrHex, 16)
}
strUnicode2Ansi = function (asContents) {
var len1 = asContents.length;
var temp = "";
for (var i = 0; i < len1; i++) {
var varasc = asContents.charCodeAt(i);
if (varasc < 0)
varasc += 65536;
if (varasc > 127)
varasc = UnicodeToAnsi(varasc);
if (varasc > 255) {
var varlow = varasc & 65280;
varlow = varlow >> 8;
var varhigh = varasc & 255;
temp += String.fromCharCode(varlow) + String.fromCharCode(varhigh);
}
else {
temp += String.fromCharCode(varasc);
}
}
return temp;
}
strAnsi2Unicode = function (asContents) {
var len1 = asContents.length;
var temp = "";
var chrcode;
for (var i = 0; i < len1; i++) {
var varasc = asContents.charCodeAt(i);
if (varasc > 127) {
chrcode = AnsiToUnicode((varasc << 8) + asContents.charCodeAt(++i));
}
else {
chrcode = varasc;
}
temp += String.fromCharCode(chrcode);
}
return temp;
}
encode64 = function (input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
keyStr.charAt(enc1) +
keyStr.charAt(enc2) +
keyStr.charAt(enc3) +
keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
decode64 = function (input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
if (input.length % 4 != 0) {
return "";
}
var base64test = /[^A-Za-z0-9/+///=]/g;
if (base64test.exec(input)) {
return "";
}
do {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output += String.fromCharCode(chr2);
}
if (enc4 != 64) {
output += String.fromCharCode(chr3);
}
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
if (opts.data == "") {
return false;
} else {
if (opts.type == 0) {
if (opts.unicode == true) {
return encode64(strUnicode2Ansi(opts.data));
} else {
return encode64(opts.data);
}
} else {
if (opts.unicode == true) {
return strAnsi2Unicode(decode64(opts.data));
} else {
return decode64(opts.data);
}
}
}
}
})(jQuery);
二、tableExport.js:
(function($){
$.fn.extend({
tableExport: function(options) {
var defaults = {
separator: ',',
ignoreColumn: [],
tableName:'yourTableName',
type:'csv',
pdfFontSize:14,
pdfLeftMargin:20,
escape:'true',
htmlContent:'false',
consoleLog:'false'
};
var options = $.extend(defaults, options);
var el = this;
if(defaults.type == 'csv' || defaults.type == 'txt'){
// Header
var tdData ="";
$(el).find('thead').find('tr').each(function() {
tdData += "\n";
$(this).filter(':visible').find('th').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
tdData += '"' + parseString($(this)) + '"' + defaults.separator;
}
}
});
tdData = $.trim(tdData);
tdData = $.trim(tdData).substring(0, tdData.length -1);
});
// Row vs Column
$(el).find('tbody').find('tr').each(function() {
tdData += "\n";
$(this).filter(':visible').find('td').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
tdData += '"'+ parseString($(this)) + '"'+ defaults.separator;
}
}
});
//tdData = $.trim(tdData);
tdData = $.trim(tdData).substring(0, tdData.length -1);
});
//output
if(defaults.consoleLog == 'true'){
console.log(tdData);
}
var base64data = "base64," + $.base64.encode(tdData);
window.open('data:application/'+defaults.type+';filename=exportData;' + base64data);
}else if(defaults.type == 'sql'){
// Header
var tdData ="INSERT INTO `"+defaults.tableName+"` (";
$(el).find('thead').find('tr').each(function() {
$(this).filter(':visible').find('th').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
tdData += '`' + parseString($(this)) + '`,' ;
}
}
});
tdData = $.trim(tdData);
tdData = $.trim(tdData).substring(0, tdData.length -1);
});
tdData += ") VALUES ";
// Row vs Column
$(el).find('tbody').find('tr').each(function() {
tdData += "(";
$(this).filter(':visible').find('td').each(function(index,data) {
if ($(this).css('display') != 'none'){
if(defaults.ignoreColumn.indexOf(index) == -1){
tdData += '"'+ parseString($(this)) + '",';
}
}
});
三、引用和导出
1.注意引入jquery.min.js,tableExport.js,mybase64.js;
2.导出 $('#ID').tableExport({ type: 'excel', escape: 'false' });
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯