csvexporter
Version:
Export your HTML tables to CSV format.
1 lines • 1.88 kB
JavaScript
!function(t){t.fn.extend({csvExport:function(e){this.defaultOptions={escapeContent:!0,title:"Exported_Table.csv",beforeStart:function(){},onStringReady:function(){}};const n=t.extend({},this.defaultOptions,e);return this.each(function(){const e=t(this),o={x:0,y:0},r={x:[],y:[]};let l="";function i(t,e){return t[e].toDo==t[e].done&&(t.splice(e,e+1),!0)}function a(){let t=!0;for(;t;){let e=!1,n=["y","x"];for(let t=0;t<n.length;t++){const a=n[t],c="y"==a?"x":"y";for(let t=0;t<r[a].length;t++)if(i(r[a],t)&&t>0&&t--,r[a].length>0&&o[c]==r[a][t].ori[c]&&o[a]==r[a][t].ori[a]+r[a][t].done){l+='"",',r[a][t].done++,e=!0,o.x++;break}}e||(t=!1)}}function c(t){return n.escapeContent?t.replace(/([\\"])/g,"\\$1"):t}function s(t,e,n){e=e||"",n=n||512;let o=atob(t),r=[],l;for(let t=0;t<o.length;t+=n){let e=o.slice(t,t+n),l=new Array(e.length);for(let t=0;t<e.length;t++)l[t]=e.charCodeAt(t);let i=new Uint8Array(l);r.push(i)}return new Blob(r,{type:e})}n.beforeStart.call(null,e),t("tr",e).each(function(){const e=t(this);e.children().each(function(){const e=t(this);a(),e.is("[colspan]")&&r.x.push({ori:{x:o.x,y:o.y},toDo:+e.attr("colspan"),done:1}),e.is("[rowspan]")&&r.y.push({ori:{x:o.x,y:o.y},toDo:+e.attr("rowspan"),done:1});let n="",i=e[0].childNodes;for(let t=0;t<i.length;t++)n+=(i[t].innerText||i[t].textContent.replace(/\s/g,"").length?i[t].textContent:"")+" ";n=c(n).trim(),l+='"'+n+'",',o.x++}),l=l.substring(0,l.length-1),l+="\r\n",o.x=0,o.y++}),n.onStringReady.call(null,l);const d=window.btoa(unescape(encodeURIComponent(l))),u=n.title;if(navigator.userAgent.indexOf("MSIE ")>0||navigator.userAgent.match(/Trident.*rv\:11\./)){const t=s(d);window.navigator.msSaveOrOpenBlob(t,u)}else{const t=document.createElement("a");t.href="data:application/vnd.ms-excel;base64,"+d,t.download=u,t.style.display="none",document.body.appendChild(t),t.click(),t.remove()}})}})}(jQuery);