title: Angular2 - Export table to html excel and give filename
body: <p>I have an <strong>Angular2</strong> application and now it was asked me to implement a <strong>basic
  table to excel export</strong>.</p>
  
  <p>The function that I'm using works but I don't know how to set the filename of the generated excel.</p>
  
  <p>This is the <strong>function</strong>:</p>
  
  <pre><code>tableToExcel(e,table, name) {
    console.log(e);
  
    let 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(decodeURI(decodeURIComponent(s))) 
       },
  format = function (s, c) {
       return s.replace(/{(\w+)}/g, 
          function (m, p) { return c[p]; 
        }) 
       }
  if (!table.nodeType) 
       table = document.getElementById(table)
  var ctx = { 
        worksheet: name || 'Worksheet', table: table.innerHTML 
  }
  //window.location.href = uri + base64(format(template, ctx))  
  console.log(uri + base64(format(template, ctx)));
  window.open(uri + base64(format(template, ctx)), '_blank', 'top=0,left=0,height=100%,width=auto');
  
    return false;
   }
  </code></pre>
  
  <p>and this is the <strong>button</strong>:</p>
  
  <pre><code>&lt;a type="button" href="#" (click)="tableToExcel($event,'testTable', 'W3C Example Table')" download="Schede.xlsx" class="btn btn-warning"&gt; Excel &lt;/a&gt;
  </code></pre>
  
  <p>In this moment I can download the file excel but the <strong>filename is completely random</strong>.</p>
  
  <p><strong>Thanks to support</strong></p>