title: How can I export a Excel file using Javascript?
body: <p>I need to export data in  '.XLSX' file for Chrome browser below code is working fine for IE.</p>
  
  <p>But code in else condition is creating download.xls file Chrome browser</p>
  
  <pre><code>sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  
  </code></pre>
  
  <p>Below is the code used on Button Click :</p>
  
  <pre><code>function fnExcelReport()
  {
      var tab_text="&lt;table border='2px'&gt;&lt;tr bgcolor='#87AFC6'&gt;";
      var textRange; var j=0;
      tab = document.getElementById('headerTable'); // id of table
  
      for(j = 0 ; j &lt; tab.rows.length ; j++) 
      {     
          tab_text=tab_text+tab.rows[j].innerHTML+"&lt;/tr&gt;";
          //tab_text=tab_text+"&lt;/tr&gt;";
      }
  
      tab_text=tab_text+"&lt;/table&gt;";
      tab_text= tab_text.replace(/&lt;A[^&gt;]*&gt;|&lt;\/A&gt;/g, "");//remove if u want links in your table
      tab_text= tab_text.replace(/&lt;img[^&gt;]*&gt;/gi,""); // remove if u want images in your table
      tab_text= tab_text.replace(/&lt;input[^&gt;]*&gt;|&lt;\/input&gt;/gi, ""); // reomves input params
  
      var ua = window.navigator.userAgent;
      var msie = ua.indexOf("MSIE "); 
  
      if (msie &gt; 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
      {
          txtArea1.document.open("txt/html","replace");
          txtArea1.document.write(tab_text);
          txtArea1.document.close();
          txtArea1.focus(); 
          sa=txtArea1.document.execCommand("SaveAs",true,"ABCD.xlsx");
      }  
      else                 //other browser not tested on IE 11
          sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  
  
      return (sa);
  }
  </code></pre>
  
  <p>Please help me on this, thanks in advance.</p>
  
  <p>Please suggest me solution with above code , I am new to javascript and unable to implement the solution in suggested link .
  </p>
  
  <p>I have also used below code to create .XLSX file, Code is creating file but file is blank and showing an error message:
  "The file Format and extension do not match.The file could be corrupted or unsafe......Do u want to opent it anyway" </p>
  
  <pre><code>var a = document.createElement('a');
  var data_type = 'data:application/vnd.ms-excel';
  var table_div = tab_text;    //Your tab_text   
  var table_html = table_div.replace(/ /g, '%20');
  a.href = data_type + ', ' + table_html;
  //setting the file name
  a.download = 'exported_table.xlsx';
  //triggering the function
  a.click();
  </code></pre>