Skip to content

Instantly share code, notes, and snippets.

@oshanz
Last active January 4, 2016 10:59
Show Gist options
  • Save oshanz/8612235 to your computer and use it in GitHub Desktop.
Save oshanz/8612235 to your computer and use it in GitHub Desktop.
client side html table export to excel
function ExportExcel(table) {
if (!table.nodeType) {
table = document.getElementById(table);
download(table.outerHTML, "ftable.xls", "application/vnd.ms-excel");
}else{
alert('not a tbl');
}
}
function download(strData, strFileName, strMimeType) {
var D = document,
a = D.createElement("a");
strMimeType = strMimeType || "application/octet-stream";
if (window.MSBlobBuilder) { //IE10+ routine
var bb = new MSBlobBuilder();
bb.append(strData);
return navigator.msSaveBlob(bb, strFileName);
} /* end if(window.MSBlobBuilder) */
if ('download' in a) { //html5 A[download]
a.href = "data:" + strMimeType + "," + encodeURIComponent(strData);
a.setAttribute("download", strFileName);
a.innerHTML = "downloading...";
D.body.appendChild(a);
setTimeout(function() {
a.click();
D.body.removeChild(a);
}, 66);
return true;
} /* end if('download' in a) */
//do iframe dataURL download (old ch+FF):
var f = D.createElement("iframe");
D.body.appendChild(f);
f.src = "data:" + strMimeType + "," + encodeURIComponent(strData);
setTimeout(function() {
D.body.removeChild(f);
}, 333);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment