{"id":10865,"date":"2021-10-29T10:00:00","date_gmt":"2021-10-29T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=10865"},"modified":"2021-10-28T19:51:54","modified_gmt":"2021-10-28T10:51:54","slug":"export-csv-file-in-js","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/10865","title":{"rendered":"Export CSV file in JS"},"content":{"rendered":"\n<p>This time I would like to share about export a file as CSV format using Javascript HandSonTable. HandSonTable  works with many of most popular languages like : React, Angualr, Vue and Javascript. <\/p>\n\n\n\n<p>Then what is HandSonTable? HandSonTable is like data grid spreadsheet designed to hundreds of thousands of cells. HandSonTable is performed with so much features ex: search, sort, filter without delays. Let&#8217;s start!<\/p>\n\n\n\n<p>Firstly, we need to do is to import handsontable.full.min.js and handsontable.full.min.css in html. After importing, we can now use HandSonTable. To display HandSonTable, we need to add a new<strong> div<\/strong> with id or class name attribute.<\/p>\n\n\n\n<p>Import JS and Css file in header<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/handsontable@10.0\/dist\/handsontable.full.min.js\"&gt;&lt;\/script&gt;\n&lt;link type=\"text\/css\" rel=\"stylesheet\" href=\"https:\/\/cdn.jsdelivr.net\/npm\/handsontable@10.0\/dist\/handsontable.full.min.css\" \/&gt; <\/code><\/pre>\n\n\n\n<p>Add new div and download button for export CSV format<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"example1\"&gt;&lt;\/div&gt;\n&lt;div class=\"controls\"&gt;\n  &lt;button id=\"export-file\"&gt;Download CSV&lt;\/button&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p>Let&#8217;s start JS code!<\/p>\n\n\n\n<p>For use HandSonTable, we must call that two attributes roughly first one is what place we want to append data so we will call  our Id named with <strong>example1<\/strong>, and second one is how much data display with <strong>rows<\/strong> and <strong>cols<\/strong> and what kind of data display. <\/p>\n\n\n\n<p>Then to call our id and button using javascript querySelector. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const container = document.querySelector('#example1');\nconst button = document.querySelector('#export-file');<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>const hot = new Handsontable(container, {\n  data: Handsontable.helper.createSpreadsheetData(7, 7),\n  colHeaders: true,\n  rowHeaders: true,\n  hiddenRows: { rows: &#091;1, 3, 5], indicators: true },\n  hiddenColumns: { columns: &#091;1, 3, 5], indicators: true },\n  height: 'auto',\n  licenseKey: 'non-commercial-and-evaluation'\n});<\/code><\/pre>\n\n\n\n<p>To export a file, we can use <strong>tabename.getPlugin(&#8216;exportFile&#8217;);<\/strong><\/p>\n\n\n\n<p>After button click action is done, for download the file we can use javascript addEventListener with <strong>click<\/strong> property. For <strong>downloadFile<\/strong> and <strong>fileExtension<\/strong> must be &#8216;csv&#8217; and  fill your desire filename.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button.addEventListener('click', () =&gt; {\n  exportPlugin.downloadFile('csv', {\n    bom: false,\n    columnDelimiter: ',',\n    columnHeaders: false,\n    exportHiddenColumns: true,\n    exportHiddenRows: true,\n    fileExtension: 'csv',\n    filename: 'Handsontable-CSV-file_&#091;YYYY]-&#091;MM]-&#091;DD]',\n    mimeType: 'text\/csv',\n    rowDelimiter: '\\r\\n',\n    rowHeaders: true\n  });\n});<\/code><\/pre>\n\n\n\n<p>Hoped you enjoyed reading this!<\/p>\n\n\n\n<p>By Ami<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10865\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/10865\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Export CSV file in JS\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10865\" data-text=\"Export CSV file in JS\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10865\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10865\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>This time I would like to share about export a file as CSV format using Javascript HandSonTable. HandSonTable  [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10865"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=10865"}],"version-history":[{"count":4,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10865\/revisions"}],"predecessor-version":[{"id":10886,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10865\/revisions\/10886"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=10865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=10865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=10865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}