{"id":340,"date":"2011-08-08T12:07:10","date_gmt":"2011-08-08T12:07:10","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=340"},"modified":"2011-08-08T12:07:10","modified_gmt":"2011-08-08T12:07:10","slug":"jquery-csv-export","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/jquery-csv-export\/","title":{"rendered":"jQuery CSV Export"},"content":{"rendered":"<p>Is it possible to do a CSV export with pure JavaScript? Close, but you still need the help of a server-side script to tell the browser what type of content is coming it&#39;s way.<\/p>\n<p>To start, we can build on a simple jQuery plugin called table2CSV. The plugin essentially converts an HTML table to a CSV string, which you can use in your export script.<\/p>\n<p>With table2CSV as a base, you could add the following jQuery snippet, which tacks an Export icon into each table caption, along with a form to post the CSV string to your processing script.<\/p>\n<p><code>$(&#39;#content table caption&#39;).each(function() {<br \/>\n\t&nbsp;&nbsp;&nbsp; d = new Date();<br \/>\n\t&nbsp;&nbsp;&nbsp; filename = $(this).html().replace(\/ \/g, &#39;-&#39;) + d.getMonth() <br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + &#39;-&#39; + d.getDate() + &#39;-&#39; + d.getFullYear() + &#39;.csv&#39;;<br \/>\n\t&nbsp;&nbsp;&nbsp; html = &#39;&lt;form action=&quot;\/csvExport.php&quot; method=&quot;post&quot;&gt;&#39;;<br \/>\n\t&nbsp;&nbsp;&nbsp; html += &#39;&lt;input type=&quot;hidden&quot; name=&quot;filename&quot; <br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value=&quot;&#39; + filename + &#39;&quot; \/&gt;&#39;;<br \/>\n\t&nbsp;&nbsp;&nbsp; html += &#39;&lt;input type=&quot;hidden&quot; name=&quot;csv_text&quot; <br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value=&quot;&quot; class=&quot;csv_text&quot; \/&gt;&#39;;<br \/>\n\t&nbsp;&nbsp;&nbsp; html += &#39;&lt;input type=&quot;image&quot; src=&quot;\/xls.png&quot; <br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alt=&quot;Export&quot; \/&gt;&#39;;<br \/>\n\t&nbsp;&nbsp;&nbsp; html += &#39;&lt;\/form&gt;&#39;;<br \/>\n\t&nbsp;&nbsp;&nbsp; $(this).append(html);<br \/>\n\t&nbsp;&nbsp;&nbsp; $(this).find(&#39;.csv_text&#39;).val(<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(this).parent().table2CSV({delivery:&#39;value&#39;}));<br \/>\n\t});<br \/>\n\t<\/code><\/p>\n<p>In PHP, your CSV export script might look like:<\/p>\n<pre><code>&lt;?php\r\nheader(&quot;Content-type: application\/octet-stream&quot;);\r\nheader(&quot;Content-Disposition: attachment; filename=\\&quot;&#39; . $_REQUEST[&#39;filename&#39;] . &#39;\\&quot;&quot;);\r\n$data=stripcslashes($_REQUEST[&#39;csv_text&#39;]);\r\necho $data; \r\n?&gt;<\/code><\/pre>\n<p>In Python, you might use:<\/p>\n<p><code>csv = context.REQUEST.get(&#39;csv_text&#39;)<br \/>\n\tfilename = context.REQUEST.get(&#39;filename&#39;)<br \/>\n\tcontext.REQUEST.RESPONSE.setHeader(&#39;Content-type&#39;,&#39;application\/msexcel&#39;)<br \/>\n\tcontext.REQUEST.RESPONSE.setHeader(&#39;Content-disposition&#39;,&#39;inline; filename=&quot;&#39; + filename + &#39;&quot;&#39;)<br \/>\n\treturn csv<br \/>\n\t<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Is it possible to do a CSV export with pure JavaScript? Close, but you still need the help of a server-side script to tell the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[5],"tags":[86,113],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/340"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=340"}],"version-history":[{"count":0,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/340\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}