Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript - export HTML table data into Excel
    primarykey
    data
    text
    <p><br />I'm trying to convert HTML tables to Excel, i have tried with a JavaScript function which converts a simple table to Excel, it is working fine. If I have multiple tables how will I be able to add all the table data into the Excel file. here's what I tried. I've created 2 tables and given table index <code>testTable</code> and <code>testTable1</code>.</p> <p>How will i pass these 2 table ids to the JavaScript function on click of the button? right now on click of the button only the first table is exported to Excel as I'm passing only <code>'testTable'</code>. how will i be able to export multiple tables eg: <code>testTable</code>, <code>testTable1</code> into Excel?</p> <p>Here's the JavaScript:</p> <pre><code>&lt;script&gt; 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;/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)) } })() &lt;/script&gt; </code></pre> <p>Here's the HTML part, </p> <pre><code>&lt;table id="testTable"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;ACP&lt;/th&gt; &lt;th&gt;OEMCP&lt;/th&gt; &lt;th&gt;Unix&lt;br&gt; NT 3.1&lt;/th&gt; &lt;th&gt;Unix&lt;br&gt; NT 3.51&lt;/th&gt; &lt;th&gt;Unix&lt;br&gt; 95&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;/table&gt; &lt;table id="testTable1"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;ACP&lt;/th&gt; &lt;th&gt;OEMCP&lt;/th&gt; &lt;th&gt;Windows&lt;br&gt; NT 3.1&lt;/th&gt; &lt;th&gt;Windows&lt;br&gt; NT 3.51&lt;/th&gt; &lt;th&gt;Windows&lt;br&gt; 95&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;/table&gt; </code></pre> <p>Please let me know, how this can be done?<br /> Thanks </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload