title: How to export html table to excel using javascript
body: <p>My table is in format </p>
  
  <pre><code>&lt;table id="mytable"&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;name&lt;/th&gt;
      &lt;th&gt;place&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
  &lt;tr&gt;
     &lt;td&gt;adfas&lt;/td&gt;
     &lt;td&gt;asdfasf&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I found the following code online. But it doesn't work if i use "thead" and "tbody"  tags</p>
  
  <pre><code>function write_to_excel() {
  
      str = "";
  
      var mytable = document.getElementsByTagName("table")[0];
      var rowCount = mytable.rows.length;
      var colCount = mytable.getElementsByTagName("tr")[0].getElementsByTagName("td").length;
  
      var ExcelApp = new ActiveXObject("Excel.Application");
      var ExcelSheet = new ActiveXObject("Excel.Sheet");
      ExcelSheet.Application.Visible = true;
  
      for (var i = 0; i &lt; rowCount; i++) {
          for (var j = 0; j &lt; colCount; j++) {
              str = mytable.getElementsByTagName("tr")[i].getElementsByTagName("td")[j].innerHTML;
              ExcelSheet.ActiveSheet.Cells(i + 1, j + 1).Value = str;
          }
      }
  </code></pre>