0

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!

Community
  • 1
  • 1
Omasu Plus
  • 229
  • 1
  • 8
  • _"but again if user export 1000++ rows data"_ What is `Blob.size` of "1000++ rows data"? Is file rendered correctly when less than one thousand rows of data are saved? – guest271314 Aug 17 '16 at 05:57
  • @guest271314 File is rendered correctly when less than one thousand rows of data are saved – Omasu Plus Aug 17 '16 at 05:59
  • You can store each file which contains less than one thousand rows of data and provide `.zip` of both files as download. See http://stackoverflow.com/questions/37176397/multiple-download-links-to-one-zip-file-before-download-javascript – guest271314 Aug 17 '16 at 06:06
  • @guest271314 blob size of 1104 rows is 1499260 – Omasu Plus Aug 17 '16 at 06:06
  • `Blob.size` of `1104` should not cause issue. What is `$(tbl).html()`? – guest271314 Aug 17 '16 at 06:07
  • Check here for potential cause of the non-opening file http://www.infoworld.com/article/3106774/microsoft-windows/good-news-for-microsoft-office-renamed-html-files-now-open-in-excel.html – Tim Williams Aug 17 '16 at 06:09
  • @guest271314 table markup,, a copy of the currently displayed table, its contents are copied to var tbl so if you take $(tbl).html() it is a string that is like "...contents...
    --end of string"
    – Omasu Plus Aug 17 '16 at 06:11
  • _"$(tbl).html() it is a string that is like "...contents.‌​..
    --end of string""_ What is purpose of rendering `` `html` as child of `
    ` at `
    '+$(tbl).html()+'
    `?
    – guest271314 Aug 17 '16 at 06:13
  • @TimWilliams thank you sir , will dig into this and hope to find a work around... – Omasu Plus Aug 17 '16 at 06:15
  • ""$(tbl).html() it is a string that is like "...contents.‌​‌​..
    --end of string"" What is purpose of rendering html as child of
    at
    '+$(tbl).htm‌​l()+'
    ?" - it was a test run and a miss-encoded html.. i also tried +$(tbl).html()+ just to try if errors in the markup causes the file to appear blank to excel but still not worked...
    – Omasu Plus Aug 17 '16 at 06:18

1 Answers1

1

https://support.microsoft.com/en-us/kb/3181507 explains it all... can now close this thread... installing the patch allowed me to view the downloaded html.xls file...

Omasu Plus
  • 229
  • 1
  • 8