title: Convert HTML table to Excel file in pure JavaScript using sheets.js plugin
body: <p>I couldn't find an existing question/answers on Stack Overflow for my particular issue so I'm posting this. I have an application written in HTML and jQuery/JavaScript. I need to export an HTML table into an Excel sheet using the jsxlsx (sheetsjs) plugin. I tried replicating their example to convert HTML table to Excel but the program errors out. Here's what I currently have:</p>
  
  <p>My HTML code is inside a coldfusion page called books.cfm. I have my JavaScript in a file called Utils.js. These are:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function s2ab(s) { 
    var buf = new ArrayBuffer(s.length); 
    var view = new Uint8Array(buf); 
    for (var i=0; i&lt;s.length; i++) 
      view[i] = s.charCodeAt(i) &amp; 0xFF; 
      return buf; 
  }
   
  function convertToExcel(event){
    event.preventDefault();
    console.log("button clicked");
    var wb = XLSX.utils.table_to_book( $("#exceltable"), {sheet:"Exported 
      table"} );
    saveAs(new Blob([s2ab(wb)],{type:"application/octet-stream"}), 
     'test.xlsx');
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt;
  &lt;head&gt;
    &lt;script src="libs/jquery.min.js"&gt;&lt;/script&gt;
    &lt;script src="libs/sheets/xlsx.full.min.js"&gt;&lt;/script&gt;
    &lt;script src="libs/FileSaver/FileSaver.min.js"&gt;&lt;/script&gt;
    &lt;script src="scripts/Utils.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;table id="myTable"&gt;
      &lt;tr&gt;
        &lt;td&gt;ID&lt;/td&gt;
        &lt;td&gt;Name&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;1&lt;/td&gt;
        &lt;td&gt;Johnny&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
   &lt;button id="myBtn" onclick="convertToExcel(event)"&gt;Export&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>When I run the above code and click on the button to convert to Excel, I get the following error:</p>
  
  <pre><code>**Uncaught TypeError: e.getElementsByTagName is not a function
  at GC (xlsx.full.min.js:20)
  at Object.jC [as table_to_book] (xlsx.full.min.js:20)
  at exportErrsToExcel (Util.js:1227)
  at HTMLButtonElement.onclick (books.cfm:18)**
  </code></pre>
  
  <p>Note: I got the above JavaScript code from <a href="https://redstapler.co/sheetjs-tutorial-html-table-to-excel/" rel="nofollow noreferrer">this site</a>.</p>