{"id":6967,"date":"2023-05-06T00:34:00","date_gmt":"2023-05-06T00:34:00","guid":{"rendered":"https:\/\/codexcoach.com\/?p=6967"},"modified":"2024-04-13T19:53:50","modified_gmt":"2024-04-13T14:23:50","slug":"how-to-export-html-table-to-csv-using-jquery","status":"publish","type":"post","link":"https:\/\/codexcoach.com\/how-to-export-html-table-to-csv-using-jquery\/","title":{"rendered":"How to Export HTML table To CSV Using jQuery"},"content":{"rendered":"\n<p>When dealing with online applications, it is typical to need to export HTML tables to CSV format. CSV is a popular format for exchanging data across programs because it is simple to read and interpret by software tools.<\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll teach you how to use jQuery to convert an HTML table to CSV format. The code samples supplied are merely for demonstration reasons, and you should modify them to fit your individual use case.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1024\" height=\"465\" src=\"https:\/\/codexcoach.com\/app\/uploads\/2023\/05\/html-table-export-using-jquery.jpg\" alt=\"\" class=\"wp-image-6982\" srcset=\"https:\/\/codexcoach.com\/app\/uploads\/2023\/05\/html-table-export-using-jquery.jpg 1024w, https:\/\/codexcoach.com\/app\/uploads\/2023\/05\/html-table-export-using-jquery-300x136.jpg 300w, https:\/\/codexcoach.com\/app\/uploads\/2023\/05\/html-table-export-using-jquery-768x349.jpg 768w, https:\/\/codexcoach.com\/app\/uploads\/2023\/05\/html-table-export-using-jquery-150x68.jpg 150w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Create an HTML table<\/h2>\n\n\n\n<p>We must first generate the HTML table that will be exported to CSV. A basic table is shown below.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-html\" data-file=\"cxc-table.html\" data-lang=\"HTML\"><code>&lt;div class=&quot;cxc_main&quot;&gt;\n   &lt;table id=&quot;cxc_exp_table&quot; class=&quot;cxc_exp_table&quot; cellpadding=&quot;5&quot; border=&quot;1&quot; cellspacing=&quot;0&quot;&gt;\n      &lt;thead class=&quot;thead&quot;&gt;\n         &lt;tr class=&quot;th-list&quot;&gt;\n            &lt;th class=&quot;cxc-th-company&quot;&gt;Cxc Company&lt;\/th&gt;\n            &lt;th class=&quot;cxc-th-address&quot;&gt;Cxc Address&lt;\/th&gt;\n            &lt;th class=&quot;cxc-th-contact&quot;&gt;Cxc Contact&lt;\/th&gt;\n            &lt;th class=&quot;cxc-th-country&quot;&gt;Cxc Country&lt;\/th&gt;\n            &lt;th class=&quot;cxc-th-zipcode&quot;&gt;Cxc Zipcode&lt;\/th&gt;\n         &lt;\/tr&gt;\n      &lt;\/thead&gt;\n      &lt;tbody class=&quot;tbody&quot;&gt;\n         &lt;tr class=&quot;tr-list&quot;&gt;\n            &lt;td class=&quot;cxc-company&quot;&gt;Codexcoach&lt;\/td&gt;\n            &lt;td class=&quot;cxc-address&quot;&gt;3807 Millbrook Road&lt;\/td&gt;\n            &lt;td class=&quot;cxc-contact&quot;&gt;Maria Anders&lt;\/td&gt;\n            &lt;td class=&quot;cxc-country&quot;&gt;Germany&lt;\/td&gt;\n            &lt;td class=&quot;cxc-zipcode&quot;&gt;60515&lt;\/td&gt;\n         &lt;\/tr&gt;\n         &lt;tr class=&quot;tr-list&quot;&gt;\n            &lt;td class=&quot;cxc-company&quot;&gt;Codexcoach&lt;\/td&gt;\n            &lt;td class=&quot;cxc-address&quot;&gt;123 India Gujarat Road&lt;\/td&gt;\n            &lt;td class=&quot;cxc-contact&quot;&gt;Maria Anders&lt;\/td&gt;\n            &lt;td class=&quot;cxc-country&quot;&gt;India&lt;\/td&gt;\n            &lt;td class=&quot;cxc-zipcode&quot;&gt;12345&lt;\/td&gt;\n         &lt;\/tr&gt;\n         &lt;tr class=&quot;tr-list&quot;&gt;\n            &lt;td class=&quot;cxc-company&quot;&gt;Codexcoach&lt;\/td&gt;\n            &lt;td class=&quot;cxc-address&quot;&gt;3807 Millbrook Road&lt;\/td&gt;\n            &lt;td class=&quot;cxc-contact&quot;&gt;Maria Anders&lt;\/td&gt;\n            &lt;td class=&quot;cxc-country&quot;&gt;Germany&lt;\/td&gt;\n            &lt;td class=&quot;cxc-zipcode&quot;&gt;60515&lt;\/td&gt;\n         &lt;\/tr&gt;\n         &lt;tr class=&quot;tr-list&quot;&gt;\n            &lt;td class=&quot;cxc-company&quot;&gt;Codexcoach&lt;\/td&gt;\n            &lt;td class=&quot;cxc-address&quot;&gt;1707 Victoria Park Ave&lt;\/td&gt;\n            &lt;td class=&quot;cxc-contact&quot;&gt;Maria Anders&lt;\/td&gt;\n            &lt;td class=&quot;cxc-country&quot;&gt;Canada&lt;\/td&gt;\n            &lt;td class=&quot;cxc-zipcode&quot;&gt;M4A 2M5&lt;\/td&gt;\n         &lt;\/tr&gt;\n         &lt;tr class=&quot;tr-list&quot;&gt;\n            &lt;td class=&quot;cxc-company&quot;&gt;Codexcoach&lt;\/td&gt;\n            &lt;td class=&quot;cxc-address&quot;&gt;3807 Millbrook Road&lt;\/td&gt;\n            &lt;td class=&quot;cxc-contact&quot;&gt;Maria Anders&lt;\/td&gt;\n            &lt;td class=&quot;cxc-country&quot;&gt;United States&lt;\/td&gt;\n            &lt;td class=&quot;cxc-zipcode&quot;&gt;60515&lt;\/td&gt;\n         &lt;\/tr&gt;\n         &lt;tr class=&quot;tr-list&quot;&gt;\n            &lt;td class=&quot;cxc-company&quot;&gt;Codexcoach&lt;\/td&gt;\n            &lt;td class=&quot;cxc-address&quot;&gt;33 Jacabina Court&lt;\/td&gt;\n            &lt;td class=&quot;cxc-contact&quot;&gt;Maria Anders&lt;\/td&gt;\n            &lt;td class=&quot;cxc-country&quot;&gt;Australia&lt;\/td&gt;\n            &lt;td class=&quot;cxc-zipcode&quot;&gt;2502&lt;\/td&gt;\n         &lt;\/tr&gt;\n         &lt;tr class=&quot;tr-list&quot;&gt;\n            &lt;td class=&quot;cxc-company&quot;&gt;Codexcoach&lt;\/td&gt;\n            &lt;td class=&quot;cxc-address&quot;&gt;Navlinskaya Ul., bld. 53&lt;\/td&gt;\n            &lt;td class=&quot;cxc-contact&quot;&gt;Maria Anders&lt;\/td&gt;\n            &lt;td class=&quot;cxc-country&quot;&gt;Russia&lt;\/td&gt;\n            &lt;td class=&quot;cxc-zipcode&quot;&gt;N\/A&lt;\/td&gt;\n         &lt;\/tr&gt;\n      &lt;\/tbody&gt;\n   &lt;\/table&gt;\n   &lt;div&gt;\n      &lt;button type=&quot;button&quot; class=&quot;cxc_exp_to_csv&quot; onclick=&quot;cxc_export_to_csv_file_fun()&quot;&gt;Export to CSV&lt;\/button&gt;  \n   &lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Write the JQuery code<\/h2>\n\n\n\n<p>we need to write the JQuery code that will handle the export process when the button is clicked. Here&#8217;s an example of how to do it:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-file=\"cxc-table.js\" data-lang=\"JavaScript\"><code>&lt;script type=&quot;text\/javascript&quot;&gt;\n\t\tfunction cxc_export_to_csv_file_fun() {\n\n\t\t\tvar data_arr = [];\n\t\t\tvar th_arr = [];\n\n\t\t\tif( jQuery(&#39;.cxc_exp_table .thead .th-list&#39;).length &gt; 0 ){\n\t\t\t\tjQuery(&#39;.cxc_exp_table .thead .th-list&#39;).each(function(){\n\t\t\t\t\tvar tr_this = jQuery(this);\n\t\t\t\t\tvar cxc_th_company = tr_this.find(&#39;.cxc-th-company&#39;).text();\n\t\t\t\t\tth_arr.push( cxc_th_company );\n\t\t\t\t\tvar cxc_th_address = tr_this.find(&#39;.cxc-th-address&#39;).text();\n\t\t\t\t\tth_arr.push( cxc_th_address );\n\t\t\t\t\tvar cxc_th_contact = tr_this.find(&#39;.cxc-th-contact&#39;).text();\n\t\t\t\t\tth_arr.push( cxc_th_contact );\n\t\t\t\t\tvar cxc_th_country = tr_this.find(&#39;.cxc-th-country&#39;).text();\n\t\t\t\t\tth_arr.push( cxc_th_country );\n\t\t\t\t\tvar cxc_th_zipcode = tr_this.find(&#39;.cxc-th-country&#39;).text();\n\t\t\t\t\tth_arr.push( cxc_th_zipcode );\n\t\t\t\t\t\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif( th_arr && th_arr.length &gt; 0 ) {\n\t\t\t\tdata_arr.push( th_arr.join(&quot;,&quot;) );\n\t\t\t}\n\n\t\t\tif( jQuery(&#39;.cxc_exp_table .tbody .tr-list&#39;).length &gt; 0 ){\n\t\t\t\tjQuery(&#39;.cxc_exp_table .tbody .tr-list&#39;).each(function(){\n\t\t\t\t\tvar tr_this = jQuery(this);\n\t\t\t\t\tvar cxc_company = tr_this.find(&#39;.cxc-company&#39;).text();\n\t\t\t\t\tcxc_company = cxc_company.trim();\n\t\t\t\t\tvar cxc_address = tr_this.find(&#39;.cxc-address&#39;).text();\n\t\t\t\t\tcxc_address = cxc_address.trim();\n\t\t\t\t\tvar cxc_contact = tr_this.find(&#39;.cxc-contact&#39;).text();\n\t\t\t\t\tcxc_contact = cxc_contact.trim();\n\t\t\t\t\tvar cxc_country = tr_this.find(&#39;.cxc-country&#39;).text();\n\t\t\t\t\tcxc_country = cxc_country.trim();\n\t\t\t\t\tvar cxc_zipcode = tr_this.find(&#39;.cxc-country&#39;).text();\n\t\t\t\t\tcxc_zipcode = cxc_zipcode.trim();\n\n\t\t\t\t\tvar data_obj = [\n\t\t\t\t\t\t&#39;&quot;&#39; + cxc_company + &#39;&quot;&#39;,\n\t\t\t\t\t\t&#39;&quot;&#39; + cxc_address + &#39;&quot;&#39;,\n\t\t\t\t\t\t&#39;&quot;&#39; + cxc_contact + &#39;&quot;&#39;,\n\t\t\t\t\t\t&#39;&quot;&#39; + cxc_country + &#39;&quot;&#39;,\n\t\t\t\t\t\t&#39;&quot;&#39; + cxc_zipcode + &#39;&quot;&#39;,\n\t\t\t\t\t\t];   \n\n\t\t\t\t\tdata_obj.map(string =&gt; string === null ? &#39;&#39; : `\\&quot;${string}\\&quot;`);                       \n\t\t\t\t\tdata_arr.push( data_obj.join(&quot;,&quot;) );\n\t\t\t\t\t\n\n\t\t\t\t});\n\t\t\t}\n\t\t\tdata_arr = data_arr.join(&#39;\\n&#39;);\n\n       \t\t\/\/ return false;\n        \t\/\/ Create CSV file object and feed\n        \t\/\/ our csv_data into it\n\t\t\tCSVFile = new Blob([data_arr], {\n\t\t\t\ttype: &quot;text\/csv&quot;\n\t\t\t});\n\n        \t\/\/ Create to temporary link to initiate\n        \t\/\/ download process\n\t\t\tvar temp_link = document.createElement(&#39;a&#39;);\n\n        \t\/\/ Download csv file\n\t\t\ttemp_link.download = &quot;Cxc Export To CSV.csv&quot;;\n\t\t\tvar url = window.URL.createObjectURL(CSVFile);\n\t\t\ttemp_link.href = url;\n\n        \t\/\/ This link should not be displayed\n\t\t\ttemp_link.style.display = &quot;none&quot;;\n\t\t\tdocument.body.appendChild(temp_link);\n\n        \t\/\/ Automatically click the link to\n        \t\/\/ trigger download\n\t\t\ttemp_link.click();\n\t\t\tdocument.body.removeChild(temp_link); \n\t\t\t\n\t\t}\n\n\t\tjQuery(document).ready( function() {\n\t\t\tjQuery(document).on( &#39;click&#39;, &#39;.cxc_exp_table .cxc_exp_to_csv&#39;, function () {\n\t\t\t\tcxc_export_to_csv_file_fun();\n\t\t\t} );\t\n\t\t} );\n\t\t\n\t&lt;\/script&gt;<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>We demonstrated how to use <a href=\"https:\/\/codexcoach.com\/category\/jquery\/\">jQuery<\/a> to convert an HTML table to CSV format. This solution is simple and straightforward to implement, and it can be tailored to different use cases by adjusting the HTML table and JavaScript code as needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1683287627435\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why would I want to export an HTML table to CSV?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>There are several reasons why you would wish to convert an HTML table to a CSV file. For example, you might offer users to download data from your online application in a format that can be readily accessed and analyzed in a spreadsheet program such as Microsoft Excel or Google Sheets.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1683287667288\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Do I need to use jQuery to export an HTML table to CSV?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No, jQuery is not required to export an HTML table to CSV. jQuery, on the other hand, provides a straightforward way to modify the DOM and handle events in JavaScript, which helps simplify the exporting process.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1683287695543\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How can I customize the CSV output format?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Because the CSV output format is defined by the data in the HTML table, you must adjust the table itself if you wish to change the format.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><div id=\"ezoic-pub-ad-placeholder-118\"><\/div>","protected":false},"excerpt":{"rendered":"<p>When dealing with online applications, it is typical to need to export HTML tables to CSV format. CSV is a popular format for exchanging data across programs because it is simple to read and interpret by software tools. In this tutorial, we&#8217;ll teach you how to use jQuery to convert an HTML table to CSV [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":6997,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_aib_schema_json_ld":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/6967"}],"collection":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/comments?post=6967"}],"version-history":[{"count":2,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/6967\/revisions"}],"predecessor-version":[{"id":13208,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/6967\/revisions\/13208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media\/6997"}],"wp:attachment":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media?parent=6967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/categories?post=6967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/tags?post=6967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}