title: Arabic encoding when exporting HTML table to Excel
body: <p>I'm using this function to export <a href="/questions/tagged/xls" class="post-tag" title="show questions tagged &#39;xls&#39;" rel="tag">xls</a> from <a href="/questions/tagged/html" class="post-tag" title="show questions tagged &#39;html&#39;" rel="tag">html</a> table</p>
  
  <pre><code>var tableToExcel = (function(e) {
      //getting values of current time for generating the file name
      var dt = new Date();
      var day = dt.getDate();
      var month = dt.getMonth() + 1;
      var year = dt.getFullYear();
      var hour = dt.getHours();
      var mins = dt.getMinutes();
      //var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
      var postfix = day + "." + month + "." + year;
      //creating a temporary HTML link element (they support setting file names)
      var a = document.createElement('a');
      //getting data from our div that contains the HTML table
      var data_type = 'data:application/vnd.ms-excel';
      var table_div = document.getElementById('reptoxlsx');
      var table_html = table_div.outerHTML.replace(/ /g, '%20');
      var sheetname = $("#chainnames").children(":selected").text();
      a.href = data_type + ', ' + table_html;
      //setting the file name
      a.download = sheetname + ' _ ' + postfix + '.xls';
      //triggering the function
      a.click();
      //just in case, prevent default behaviour
      //e.preventDefault();
      return false;
  });
  </code></pre>
  
  <p>after a lot of search about (how to export data in Arabic format) I found that I have to change Charset of the file, So, I found that code</p>
  
  <pre><code>var uri = 'data:application/vnd.ms-excel;charset=UTF-8;base64,'
  </code></pre>
  
  <ul>
  <li>ref: <a href="https://stackoverflow.com/a/42661534/2080202">https://stackoverflow.com/a/42661534/2080202</a></li>
  </ul>
  
  <p>So I added to my code:</p>
  
  <pre><code>var data_type = 'data:application/vnd.ms-excel;charset=UTF-8;base64';
  </code></pre>
  
  <p>but when I tried to export XLS file from OnClick event:</p>
  
  <pre><code>$(document).on('click','#exportreptoexcel',function(){
          tableToExcel();
      });
  </code></pre>
  
  <p>the file (browser) gives me:</p>
  
  <blockquote>
    <p>Failed - Network Error</p>
  </blockquote>
  
  <p>So I edit mine by deleting <strong>;base64</strong> changing the code to:</p>
  
  <pre><code>var data_type = 'data:application/vnd.ms-excel;charset=UTF-8';
  </code></pre>
  
  <p>Exporting/Downloading the file was great, but Arabic still the same:</p>
  
  <p><a href="https://i.stack.imgur.com/UtfQd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UtfQd.png" alt="Arabic issue in XLS after Export from HTML table"></a></p>
  
  <p>So, the question is, how to export Arabic in Excel using the giving code?</p>
  
  <p>thanks in advance</p>
  
  <p><hr>
  <strong>EDIT 1</strong>
  I found another <a href="/questions/tagged/script" class="post-tag" title="show questions tagged &#39;script&#39;" rel="tag">script</a> and it worked greaaat with Arabic charset, BUT without supporting file name variable.</p>
  
  <pre><code>var tableToExcel = (function() {
    var uri = 'data:application/vnd.ms-excel;base64,'
      , template = '&lt;html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"&gt;&lt;head&gt;&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;&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;&lt;meta http-equiv="content-type" content="text/plain; charset=UTF-8"/&gt;&lt;/head&gt;&lt;body&gt;&lt;table&gt;{table}&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;'
      , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
      , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
    return function(table, name) {
      if (!table.nodeType) table = document.getElementById(table)
      var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
      window.location.href = uri + base64(format(template, ctx))
    }
  })()
  </code></pre>
  
  <p>to export <a href="/questions/tagged/xls" class="post-tag" title="show questions tagged &#39;xls&#39;" rel="tag">xls</a> file, just call it in OnClick event:</p>
  
  <pre><code>$(document).on('click','#exportreptoexcelfile',function(){
      //working great with Arabic without filename
      var sheetname = $("#chainnames").children(":selected").text();
      tableToExcel('reptoxlsx',sheetname);
  
  });
  </code></pre>
  
  <p>-- the Question is: How to make this Function support "fileName" as a variable?!</p>
  
  <p>thanks again</p>