i am trying to save a <table>...</table>
html markup as an excel file (so it is basically a table html in a file with .xls extension), i have a code i got from the internet(credits to owner and thanks!) which works for IE.
My problem now is, clients use google chrome since it does the export faster (and lower file size), after months since deployment the file downloaded is now not being read by ms excel. The file is downloaded but when opened with ms-excel nothing appears not even a warning that the file is corrupt or file format is unknown try to open? message. Funny thing is when opened with notepad++ the markup can be viewed, delete a space then re-enter it then save>open in excel and viola it now can be viewed..
What i have:
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
txtArea1.document.open("txt/html","replace");
txtArea1.document.write($(tbl).html());
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand("SaveAs",true,"toExcel.xls");
return (sa);
}
else
{
try{
var blob = new Blob([$(tbl).html()]);
var blobURL = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = blobURL;
a.download = 'toExcel.xls';
a.click();
} catch(err){
//console.log(err.message);
alert(err.message);
}
}
What i tried (still cannot be read by ms-excel):
var blob = new Blob([$(tbl).html()],{type:'application/vnd.ms-excel'});
/*-----------------------------------------------------------------------*/
var blob = new Blob([$(tbl).html()],{type:"application/vnd.ms-excel"});
var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
window.open(reader.result);
} //this code works but if users export 1000++ rows of data it fails //URL max limit?
/*---------------------------------------------------------------------*/
var blob = new Blob(['\ufeff', $(tbl).html()]);
/*---------------------------------------------------------------------*/
var blob = new Blob(['<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" \n\
xmlns="http://www.w3.org/TR/REC-html40"><head>\n\
<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>\n\
<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->\n\
</head><body><table>'+$(tbl).html()+'</table></body></html>'],{type:"application/vnd.ms-excel"});
/*-----------------------------------------------------------*/
i also tried window.open('data:application/vnd.ms-excel'...)
but again if user export 1000++ rows data this will not work...
Even heard and tried FileSaver.js but still not resolved the issue file is downloaded but cannot be opened by excel unless open>edit>save on np++...
Also i am using Google Chrome Version 52.0.2743.116 m ...
Last Resort(will need/have to overhaul the code...): Joel Coehoorn's Answer
Any thoughts/suggestions thanks!