Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Demo</h1> <p>See below for an explanation.</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>$(document).ready(function() { function exportTableToCSV($table, filename) { var $rows = $table.find('tr:has(td)'), // Temporary delimiter characters unlikely to be typed by keyboard // This is to avoid accidentally splitting the actual contents tmpColDelim = String.fromCharCode(11), // vertical tab character tmpRowDelim = String.fromCharCode(0), // null character // actual delimiter characters for CSV format colDelim = '","', rowDelim = '"\r\n"', // Grab text from table into CSV formatted string csv = '"' + $rows.map(function(i, row) { var $row = $(row), $cols = $row.find('td'); return $cols.map(function(j, col) { var $col = $(col), text = $col.text(); return text.replace(/"/g, '""'); // escape double quotes }).get().join(tmpColDelim); }).get().join(tmpRowDelim) .split(tmpRowDelim).join(rowDelim) .split(tmpColDelim).join(colDelim) + '"'; // Deliberate 'false', see comment below if (false &amp;&amp; window.navigator.msSaveBlob) { var blob = new Blob([decodeURIComponent(csv)], { type: 'text/csv;charset=utf8' }); // Crashes in IE 10, IE 11 and Microsoft Edge // See MS Edge Issue #10396033 // Hence, the deliberate 'false' // This is here just for completeness // Remove the 'false' at your own risk window.navigator.msSaveBlob(blob, filename); } else if (window.Blob &amp;&amp; window.URL) { // HTML5 Blob var blob = new Blob([csv], { type: 'text/csv;charset=utf-8' }); var csvUrl = URL.createObjectURL(blob); $(this) .attr({ 'download': filename, 'href': csvUrl }); } else { // Data URI var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); $(this) .attr({ 'download': filename, 'href': csvData, 'target': '_blank' }); } } // This must be a hyperlink $(".export").on('click', function(event) { // CSV var args = [$('#dvData&gt;table'), 'export.csv']; exportTableToCSV.apply(this, args); // If CSV, don't do event.preventDefault() or return false // We actually need this to be a typical hyperlink }); });</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>a.export, a.export:visited { display: inline-block; text-decoration: none; color: #000; background-color: #ddd; border: 1px solid #ccc; padding: 8px; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;a href="#" class="export"&gt;Export Table data into Excel&lt;/a&gt; &lt;div id="dvData"&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Column One&lt;/th&gt; &lt;th&gt;Column Two&lt;/th&gt; &lt;th&gt;Column Three&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;row1 Col1&lt;/td&gt; &lt;td&gt;row1 Col2&lt;/td&gt; &lt;td&gt;row1 Col3&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;row2 Col1&lt;/td&gt; &lt;td&gt;row2 Col2&lt;/td&gt; &lt;td&gt;row2 Col3&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;row3 Col1&lt;/td&gt; &lt;td&gt;row3 Col2&lt;/td&gt; &lt;td&gt;row3 Col3&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;row4 'Col1'&lt;/td&gt; &lt;td&gt;row4 'Col2'&lt;/td&gt; &lt;td&gt;row4 'Col3'&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;row5 &amp;quot;Col1&amp;quot;&lt;/td&gt; &lt;td&gt;row5 &amp;quot;Col2&amp;quot;&lt;/td&gt; &lt;td&gt;row5 &amp;quot;Col3&amp;quot;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;row6 "Col1"&lt;/td&gt; &lt;td&gt;row6 "Col2"&lt;/td&gt; &lt;td&gt;row6 "Col3"&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt;</code></pre> </div> </div> </p> <hr> <h1>As of 2017</h1> <p>Now uses HTML5 <code>Blob</code> and <code>URL</code> as the preferred method with <code>Data URI</code> as a fallback.</p> <h1>On Internet Explorer</h1> <p>Other answers suggest <code>window.navigator.msSaveBlob</code>; however, it is known to <strong>crash IE10/Window 7 and IE11/Windows 10</strong>. Whether it works using Microsoft Edge is dubious (see <a href="https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10396033/" rel="noreferrer">Microsoft Edge issue ticket #10396033</a>).</p> <p>Merely calling this in Microsoft's own Developer Tools / Console causes the browser to crash:</p> <pre><code>navigator.msSaveBlob(new Blob(["hello"], {type: "text/plain"}), "test.txt"); </code></pre> <p>
Four years after my first answer, new IE versions include IE10, IE11, and Edge. They all crash on a function that Microsoft invented (slow clap).</p> <blockquote> <p>Add <code>navigator.msSaveBlob</code> support at your own risk.</p> </blockquote> <hr> <h1>As of 2013</h1> <p>Typically this would be performed using a server-side solution, but this is my attempt at a client-side solution. Simply dumping HTML as a <code>Data URI</code> will not work, but is a helpful step. So:</p> <ol> <li>Convert the table contents into a valid CSV formatted string. (This is the easy part.)</li> <li>Force the browser to download it. The <code>window.open</code> approach would not work in Firefox, so I used <code>&lt;a href="{Data URI here}"&gt;</code>.</li> <li>Assign a default file name using the <code>&lt;a&gt;</code> tag's <code>download</code> attribute, which only works in Firefox and Google Chrome. Since it is just an attribute, it degrades gracefully.</li> </ol> <hr> <h1>Notes</h1> <ul> <li>You can style your link to look like a button. I'll leave this effort to you</li> <li><strong>IE has Data URI restrictions</strong>. See: <a href="https://stackoverflow.com/questions/7405345/data-uri-scheme-and-internet-explorer-9-errors">Data URI scheme and Internet Explorer 9 Errors</a></li> <li><p>About the <strong>"download" attribute</strong>, see these:</p> <ul> <li><a href="http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download" rel="noreferrer">Downloading resources in HTML5: a[download]</a></li> <li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=676619" rel="noreferrer">Bug 676619 - Implement proposed download attribute</a></li> <li><a href="https://stackoverflow.com/questions/12112844/how-to-detect-support-for-the-html5-download-attribute">How to detect support for the HTML5 “download” attribute</a></li> </ul></li> </ul> <h1>Compatibility</h1> <p>Browsers testing includes:</p> <ul> <li>Firefox 20+, Win/Mac (<strong>works</strong>)</li> <li>Google Chrome 26+, Win/Mac (<strong>works</strong>)</li> <li>Safari 6, Mac (<strong>works</strong>, but filename is ignored)</li> <li>IE 9+ (<strong>fails</strong>)</li> </ul> <h1>Content Encoding</h1> <p>The CSV is exported correctly, but when imported into Excel, the character <code>ü</code> is printed out as <code>ä</code>. Excel interprets the value incorrectly.</p> <p>Introduce <code>var csv = '\ufeff';</code> and then Excel 2013+ interprets the values correctly.</p> <p>If you need compatibility with Excel 2007, add UTF-8 prefixes at each data value. See also:</p> <ul> <li><a href="https://stackoverflow.com/q/6002256/59087">Is it possible to force Excel recognize UTF-8 CSV files automatically?</a></li> <li><a href="https://stackoverflow.com/q/155097/59087">Microsoft Excel mangles Diacritics in .csv files?</a></li> <li><a href="https://stackoverflow.com/q/17879198/59087">Adding UTF-8 BOM to string/Blob</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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