diff --git a/README.md b/README.md index 2cde266..9737e2e 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,10 @@ Options ======= ```javascript -csvSeparator: ',' -csvEnclosure: '"' consoleLog: false +csvEnclosure: '"' +csvSeparator: ',' +csvUseBOM: true displayTableName: false escape: false excelstyles: ['css','properties','to','export','to','excel'] diff --git a/tableExport.js b/tableExport.js index 0d68b8a..97e8b0e 100644 --- a/tableExport.js +++ b/tableExport.js @@ -25,9 +25,10 @@ $.fn.extend({ tableExport: function (options) { var defaults = { - csvSeparator: ',', - csvEnclosure: '"', consoleLog: false, + csvEnclosure: '"', + csvSeparator: ',', + csvUseBOM: true, displayTableName: false, escape: false, excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'], @@ -118,12 +119,12 @@ return base64encode(csvData); try { - var blob = new Blob([(defaults.type == 'csv' ? '\ufeff' : '') + csvData], {type: "text/" + (defaults.type == 'csv' ? 'csv' : 'plain') + ";charset=utf-8"}); + var blob = new Blob([((defaults.type == 'csv' && defaults.csvUseBOM)? '\ufeff' : '') + csvData], {type: "text/" + (defaults.type == 'csv' ? 'csv' : 'plain') + ";charset=utf-8"}); saveAs(blob, defaults.fileName + '.' + defaults.type); } catch (e) { downloadFile(defaults.fileName + '.' + defaults.type, - 'data:text/' + (defaults.type == 'csv' ? 'csv' : 'plain') + ';charset=utf-8,' + (defaults.type == 'csv' ? '\ufeff' : '') + + 'data:text/' + (defaults.type == 'csv' ? 'csv' : 'plain') + ';charset=utf-8,' + ((defaults.type == 'csv' && defaults.csvUseBOM )? '\ufeff' : '') + encodeURIComponent(csvData)); } diff --git a/tableExport.min.js b/tableExport.min.js index a0f732d..cf6972d 100644 --- a/tableExport.min.js +++ b/tableExport.min.js @@ -4,21 +4,21 @@ z(k.output()))}}function I(k,d,e){var h="";if(null!=k)if(d=v(k,d,e),k=null===d|| ""),!0===a.escape&&(h=escape(h)),"function"===typeof a.onCellData&&(h=a.onCellData(k,d,e,h)));return h}function P(a,d,e){return d+"-"+e.toLowerCase()}function B(a,d){try{return window.getComputedStyle?(d=d.replace(/([a-z])([A-Z])/,P),window.getComputedStyle(a,null).getPropertyValue(d)):a.currentStyle?a.currentStyle[d]:a.style[d]}catch(e){}return""}function G(a,d,e){d=B(a,d).match(/\d+/);if(null!==d){d=d[0];var h=document.createElement("div");h.style.overflow="hidden";h.style.visibility="hidden";a.parentElement.appendChild(h); h.style.width=100+e;e=100/h.offsetWidth;a.parentElement.removeChild(h);return d*e}return 0}function A(a,d){var e=document.createElement("a");if(e){document.body.appendChild(e);e.style="display: none";e.download=a;e.href=d;if(document.createEvent)null==E&&(E=document.createEvent("MouseEvents")),E.initEvent("click",!0,!1),e.dispatchEvent(E);else if(document.createEventObject)e.fireEvent("onclick");else if("function"==typeof e.onclick)e.onclick();document.body.removeChild(e)}}function z(a){var d="", e,h,b,f,g,m,l=0;a=a.replace(/\x0d\x0a/g,"\n");h="";for(b=0;bf?h+=String.fromCharCode(f):(127f?h+=String.fromCharCode(f>>6|192):(h+=String.fromCharCode(f>>12|224),h+=String.fromCharCode(f>>6&63|128)),h+=String.fromCharCode(f&63|128));for(a=h;l>2,e=(e&3)<<4|h>>4,g=(h&15)<<2|b>>6,m=b&63,isNaN(h)?g=m=64:isNaN(b)&&(m=64),d=d+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(f)+ -"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(e)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(g)+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(m);return d}var a={csvSeparator:",",csvEnclosure:'"',consoleLog:!1,displayTableName:!1,escape:!1,excelstyles:["border-bottom","border-top","border-left","border-right"],fileName:"tableExport",htmlContent:!1,ignoreColumn:[],ignoreRow:[],jspdf:{orientation:"p",unit:"pt",format:"a4", -margins:{left:20,right:10,top:10,bottom:10},autotable:{padding:2,lineHeight:12,fontSize:8,tableExport:{onAfterAutotable:null,onBeforeAutotable:null,onTable:null}}},onCellData:null,outputMode:"file",tbodySelector:"tr",theadSelector:"tr",tableName:"myTableName",type:"csv",worksheetName:"xlsWorksheetName"},E=null,n=0,y=[],g="";f.extend(!0,a,t);if("csv"==a.type||"txt"==a.type){var p="",n=0;f(this).find("thead").first().find(a.theadSelector).each(function(){g="";u(this,"th,td",n,function(k,d,e){g+=I(k, -d,e)+a.csvSeparator});g=f.trim(g).substring(0,g.length-1);0',q=q+"";f(this).find("thead").first().find(a.theadSelector).each(function(){u(this,"th,td",n,function(a,d,e){q+=""+v(a,d,e)+""});n++});var q=q+"",L=1;f(this).find("tbody").first().find(a.tbodySelector).each(function(){var a=1;g="";u(this,"td",n,function(d,e,h){g+=""+v(d,e,h)+ -"";a++});0"!=g&&(q+=''+g+"",L++);n++});q+="";!0===a.consoleLog&&console.log(q);if("string"==a.outputMode)return q;t=z(q);if("base64"==a.outputMode)return t;try{w=new Blob([q],{type:"application/xml;charset=utf-8"}),saveAs(w,a.fileName+".xml")}catch(T){A(a.fileName+".xml","data:application/xml;charset=utf-8;base64,"+t)}}else if("excel"==a.type||"doc"==a.type){var n=0,x="";f(this).find("thead").first().find(a.theadSelector).each(function(){g= -"";u(this,"th,td",n,function(b,d,e){if(null!=b){g+=""}});0"+g+"");n++});f(this).find("tbody").first().find(a.tbodySelector).each(function(){g="";u(this,"td",n,function(b,d,e){if(null!=b){g+=""}});0"+g+"");n++});a.displayTableName&&(x+="");x+="
"+v(b,d,e)+"
"+v(b,d,e)+"
"+v(f("

"+a.tableName+"

"))+"
";!0===a.consoleLog&&console.log(x);b=""; -b+='';b=b+'';b+="";"excel"===a.type&&(b+="\x3c!--[if gte mso 9]>",b+="",b+="",b+="",b+="",b+="",b+=a.worksheetName,b+="",b+="",b+="",b+="",b+="", -b+="",b+="",b+="",b+="";b+="";b+=x;b+="";b+="";if("string"==a.outputMode)return b;t=z(b);if("base64"===a.outputMode)return t;var M="excel"===a.type?"xls":"doc";try{w=new Blob([b],{type:"application/vnd.ms-"+a.type}),saveAs(w,a.fileName+"."+M)}catch(U){A(a.fileName+"."+M,"data:application/vnd.ms-"+a.type+";base64,"+t)}}else if("png"==a.type)html2canvas(f(this),{onrendered:function(b){b=b.toDataURL();b=b.substring(22); -for(var d=atob(b),e=new ArrayBuffer(d.length),h=new Uint8Array(e),f=0;fO){a>C.a0[0]&&(F="a0",D="l");for(var b in C)C.hasOwnProperty(b)&&C[b][1]>a&&(F=b,D="l",C[b][0]>a&& -(D="p"));O=a}}});a.jspdf.format=""==F?"a4":F;a.jspdf.orientation=""==D?"w":D}l.doc=new jsPDF(a.jspdf.orientation,a.jspdf.unit,a.jspdf.format);f(this).filter(":visible").each(function(){if("none"!=f(this).css("display")){var b,d=0,e={};l.columns=[];l.rows=[];l.rowoptions={};if("function"===typeof l.onTable&&!1===l.onTable(f(this),a))return!0;Object.keys(a.jspdf.autotable).forEach(function(b){e[b]=a.jspdf.autotable[b]});e.margins={};f.extend(!0,e.margins,a.jspdf.margins);"function"!==typeof e.renderCell&& -(e.renderCell=function(a,b,e,d,f,g,m,k){var l=k.tableExport.doc,n=0;l.setFillColor(0===m%2?245:255);l.setTextColor(50);l.rect(a,b,e,d,"F");b+=k.lineHeight/2+l.autoTableTextHeight()/2-2.5;"undefined"!=typeof k.tableExport.columns[f]&&(d=k.tableExport.columns[f],"undefined"!=typeof d.style&&(d=d.style.align,f=k.tableExport.rowoptions[m+1+":"+f],"undefined"!=typeof f&&(d=f.style.align),"right"==d?n=e-l.getStringUnitWidth(""+g)*l.internal.getFontSize()-k.padding:"center"==d&&(n=(e-l.getStringUnitWidth(""+ -g)*l.internal.getFontSize())/2)));n',q=q+"";f(this).find("thead").first().find(a.theadSelector).each(function(){u(this,"th,td",n,function(a,d,e){q+=""+v(a,d,e)+""});n++});var q=q+"",L=1;f(this).find("tbody").first().find(a.tbodySelector).each(function(){var a= +1;g="";u(this,"td",n,function(d,e,h){g+=""+v(d,e,h)+"";a++});0"!=g&&(q+=''+g+"",L++);n++});q+="";!0===a.consoleLog&&console.log(q);if("string"==a.outputMode)return q;t=z(q);if("base64"==a.outputMode)return t;try{w=new Blob([q],{type:"application/xml;charset=utf-8"}),saveAs(w,a.fileName+".xml")}catch(T){A(a.fileName+".xml","data:application/xml;charset=utf-8;base64,"+t)}}else if("excel"==a.type|| +"doc"==a.type){var n=0,x="";f(this).find("thead").first().find(a.theadSelector).each(function(){g="";u(this,"th,td",n,function(b,d,e){if(null!=b){g+=""}});0"+g+"");n++});f(this).find("tbody").first().find(a.tbodySelector).each(function(){g="";u(this,"td",n,function(b,d,e){if(null!=b){g+=""}});0"+g+"");n++});a.displayTableName&&(x+="");x+="
"+v(b,d,e)+"
"+v(b,d,e)+"
"+v(f("

"+a.tableName+"

"))+"
";!0===a.consoleLog&&console.log(x);b="";b+='';b=b+'';b+="";"excel"===a.type&&(b+="\x3c!--[if gte mso 9]>",b+="",b+="",b+="",b+="",b+="",b+=a.worksheetName,b+="",b+="",b+="", +b+="",b+="",b+="",b+="",b+="",b+="";b+="";b+=x;b+="";b+="";if("string"==a.outputMode)return b;t=z(b);if("base64"===a.outputMode)return t;var M="excel"===a.type?"xls":"doc";try{w=new Blob([b],{type:"application/vnd.ms-"+a.type}),saveAs(w,a.fileName+"."+M)}catch(U){A(a.fileName+"."+M,"data:application/vnd.ms-"+a.type+";base64,"+t)}}else if("png"==a.type)html2canvas(f(this), +{onrendered:function(b){b=b.toDataURL();b=b.substring(22);for(var d=atob(b),e=new ArrayBuffer(d.length),h=new Uint8Array(e),f=0;fO){a>C.a0[0]&&(F="a0",D="l");for(var b in C)C.hasOwnProperty(b)&& +C[b][1]>a&&(F=b,D="l",C[b][0]>a&&(D="p"));O=a}}});a.jspdf.format=""==F?"a4":F;a.jspdf.orientation=""==D?"w":D}l.doc=new jsPDF(a.jspdf.orientation,a.jspdf.unit,a.jspdf.format);f(this).filter(":visible").each(function(){if("none"!=f(this).css("display")){var b,d=0,e={};l.columns=[];l.rows=[];l.rowoptions={};if("function"===typeof l.onTable&&!1===l.onTable(f(this),a))return!0;Object.keys(a.jspdf.autotable).forEach(function(b){e[b]=a.jspdf.autotable[b]});e.margins={};f.extend(!0,e.margins,a.jspdf.margins); +"function"!==typeof e.renderCell&&(e.renderCell=function(a,b,e,d,f,g,m,k){var l=k.tableExport.doc,n=0;l.setFillColor(0===m%2?245:255);l.setTextColor(50);l.rect(a,b,e,d,"F");b+=k.lineHeight/2+l.autoTableTextHeight()/2-2.5;"undefined"!=typeof k.tableExport.columns[f]&&(d=k.tableExport.columns[f],"undefined"!=typeof d.style&&(d=d.style.align,f=k.tableExport.rowoptions[m+1+":"+f],"undefined"!=typeof f&&(d=f.style.align),"right"==d?n=e-l.getStringUnitWidth(""+g)*l.internal.getFontSize()-k.padding:"center"== +d&&(n=(e-l.getStringUnitWidth(""+g)*l.internal.getFontSize())/2)));n