title: Javascript Export Table to Excel
body: <p>I am trying to output a table to excel using JavaScript. For some reason, it will output the first row, which is the column headings, but it doesn't get the subsequent data rows. </p>
  
  <p>This is the code that gets the information from a php page, and then outputs it to the screen in a table.</p>
  
  <pre><code>   jQuery(document).ready(function () {
      range = "30days";
      $.ajax({
          type: 'POST',
          url: 'getHash.php',
          data: 'value=' + range,
          dataType: 'json',
          cache: false,
          success: function(result) {
              // We get the results back and store details;
              hash = result;
              //We need to clear the text already on the screen
              $("#hashDetails").text("");
  
              $("#hashDetails").append("&lt;br/&gt;&lt;br/&gt;&lt;table width=1400 align=center id='hashTable'&gt;&lt;tr&gt;&lt;td align=center width=200&gt;&lt;b&gt;UPC&lt;/b&gt;&lt;/td&gt;&lt;td align=center width=200&gt;&lt;b&gt;TAG&lt;/b&gt;&lt;/td&gt;&lt;td align=center width=200&gt;&lt;b&gt;SKU&lt;/b&gt;&lt;/td&gt;&lt;td align=center width=200&gt;&lt;b&gt;PKG&lt;/b&gt;&lt;/td&gt;&lt;td align=center width=200&gt;&lt;b&gt;LOCATION&lt;/b&gt;&lt;/td&gt;&lt;td align=center width=200&gt;&lt;b&gt;QUANTITY&lt;/b&gt;&lt;/td&gt;&lt;td align=center width=200&gt;&lt;b&gt;LAST TIME SHIPPED&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;");
              for (var i = 0; i &lt; hash.length; i++) {
                  $("#hashDetails").append("&lt;tr class='bottom'&gt;&lt;td width=200 align=center class='bottom'&gt;" + hash[i][0] + "&lt;/td&gt;&lt;td width=200 align=center class='bottom'&gt;" + hash[i][1] + "&lt;/td&gt;&lt;td width=200 align=center  class='bottom'&gt; " + hash[i][2] +"&lt;/td&gt;&lt;/td&gt;&lt;td width=200 align=center  class='bottom'&gt; " + hash[i][3] +"&lt;/td&gt;&lt;/td&gt;&lt;td width=200 align=center  class='bottom'&gt; " + hash[i][4] +"&lt;/td&gt;&lt;/td&gt;&lt;td width=200 align=center  class='bottom'&gt; " + hash[i][5] +"&lt;/td&gt;&lt;/td&gt;&lt;td width=200 align=center  class='bottom'&gt; " + hash[i][6] +"&lt;/td&gt;&lt;/tr&gt;");
              }
              $("#hashDetails").append("&lt;/table&gt;");
          }   ,
      });
  
      $('input[type=radio]').live('change', function() { alert('error'); });
  
  });
  </code></pre>
  
  <p>This is the code that exports the table to Excel:</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>For the most part, it does work, as in it downloads an excel spreadsheet to the customer's computer. When I try to open the spreadsheet it says:</p>
  
  <p>" The file you are trying to open, download.xls, is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"</p>
  
  <p>If I click Yes, it opens the file with just the column headings in row 1:</p>
  
  <p>UPC TAG SKU PKG LOCATION    QUANTITY    LAST TIME SHIPPED</p>