title: Google Chrome + Javascript[jquery]: Blob to Excel
body: <p>i am trying to save a <code>&lt;table&gt;...&lt;/table&gt;</code> 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.</p>
  
  <p>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..</p>
  
  <p>What i have:</p>
  
  <pre><code>if (msie &gt; 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);            
      }        
  }
  </code></pre>
  
  <p>What i tried (still cannot be read by ms-excel):</p>
  
  <pre><code>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(['&lt;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"&gt;&lt;head&gt;\n\
                              &lt;!--[if gte mso 9]&gt;&lt;xml&gt;&lt;x:ExcelWorkbook&gt;&lt;x:ExcelWorksheets&gt;&lt;x:ExcelWorksheet&gt;&lt;x:Name&gt;{worksheet}&lt;/x:Name&gt;&lt;x:WorksheetOptions&gt;\n\
                              &lt;x:DisplayGridlines/&gt;&lt;/x:WorksheetOptions&gt;&lt;/x:ExcelWorksheet&gt;&lt;/x:ExcelWorksheets&gt;&lt;/x:ExcelWorkbook&gt;&lt;/xml&gt;&lt;![endif]--&gt;\n\
                              &lt;/head&gt;&lt;body&gt;&lt;table&gt;'+$(tbl).html()+'&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;'],{type:"application/vnd.ms-excel"}); 
  /*-----------------------------------------------------------*/
  </code></pre>
  
  <p>i also tried <code>window.open('data:application/vnd.ms-excel'...)</code> but again if user export 1000++ rows data this will not work...</p>
  
  <p>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++...</p>
  
  <p>Also i am using Google Chrome Version 52.0.2743.116 m ...</p>
  
  <p>Last Resort(will need/have to overhaul the code...):
  <a href="https://stackoverflow.com/questions/150339/generating-an-excel-file-in-asp-net">Joel Coehoorn's Answer</a></p>
  
  <p>Any thoughts/suggestions thanks!</p>