{"id":373222,"date":"2023-03-29T06:32:49","date_gmt":"2023-03-29T06:32:49","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=373222"},"modified":"2023-03-29T06:39:30","modified_gmt":"2023-03-29T06:39:30","slug":"create-download-excel-html-and-text-files-using-javascript","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/","title":{"rendered":"Create &amp; Download Excel, HTML, and Text files using JavaScript."},"content":{"rendered":"\n<p>In this blog, we will look at how to generate and download excel, html, and text files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Create &amp; Download a excel(.xlsx) file using javascript.<\/h3>\n\n\n\n<p>Prepare your data in an array.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ Some dummy data for user details.\nlet data = &#091;\n    &#091; &#039;Id&#039;, &#039;FirstName&#039;, &#039;LastName&#039;, &#039;Mobile&#039;, &#039;Address&#039; ], \/\/ This is your header.\n    &#091; 1, &#039;Richard&#039;, &#039;Roe&#039;, &#039;9874563210&#039;, &#039;Address&#039; ],\n    &#091; 2, &#039;Joe&#039;, &#039;Doakes&#039;, &#039;7896541230&#039;, &#039;Address&#039; ],\n    &#091; 3, &#039;John&#039;, &#039;Smith&#039;, &#039;8745632109&#039;, &#039;Address&#039; ],\n    &#091; 4, &#039;Joe&#039;, &#039;Sixpack&#039;, &#039;9875647890&#039;, &#039;Address&#039; ],\n    &#091; 5, &#039;Richard&#039;, &#039;Thomson&#039;, &#039;8632547890&#039;, &#039;Address&#039; ]\n];<\/pre>\n\n\n\n<p>Prepare data for excel.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">let excelData = &#039;&#039;;\n\n\/\/ Prepare data for excel.You can also use html tag for create table for excel.\ndata.forEach(( rowItem, rowIndex ) =&gt; {   \n    \n    if (0 === rowIndex) {\n        \/\/ This is for header.\n\trowItem.forEach((colItem, colIndex) =&gt; {\n\t     excelData += colItem + &#039;,&#039;;\n\t});\n\texcelData += &quot;\\r\\n&quot;;\n\n     } else {\n        \/\/ This is data.\n        rowItem.forEach((colItem, colIndex) =&gt; {\n\t   excelData += colItem + &#039;,&#039;;   \n\t})\n       excelData += &quot;\\r\\n&quot;;       \n     }\n});<\/pre>\n\n\n\n<p>We have completed the preparing data. Now move towards creating a blob URL and download file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ Create the blob url for the file. \nexcelData = &quot;data:text\/xlsx,&quot; + encodeURI(excelData);\n\n\/\/ Download the xlsx file.\nlet a = document.createElement(&quot;A&quot;);\na.setAttribute(&quot;href&quot;, excelData);\na.setAttribute(&quot;download&quot;, &quot;filename.xlsx&quot;);\ndocument.body.appendChild(a);\na.click();<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Create &amp; Download a html(.html) file using javascript.<\/h3>\n\n\n\n<p>Prepare data for HTML.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ I am creating table but you can create any design as per your need.\n\nlet htmlData = `&lt;table align=&quot;center&quot; border=&quot;1&quot;&gt;`;\n    \ndata.forEach(( rowItem, rowIndex ) =&gt; {\n   if (0 === rowIndex) {\n       htmlData += `&lt;tr&gt;`;\n       rowItem.forEach(( colItem, colIndex ) =&gt; {\n          htmlData += `&lt;th&gt;${colItem}&lt;\/th&gt;`;\n       });\n       htmlData += `&lt;\/tr&gt;`;\n   } else {\n       htmlData += `&lt;tr&gt;`;\n       rowItem.forEach(( colItem, colIndex ) =&gt; {\n            htmlData += `&lt;td&gt;${colItem}&lt;\/td&gt;`;\n       });\n       htmlData += `&lt;\/tr&gt;`;\n    }\n});<\/pre>\n\n\n\n<p>We have completed the preparing data for  HTML. Now move towards creating a blob URL and download file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ Create the blob url for the file.\nhtmlData = &quot;data:text\/html,&quot; + encodeURI(htmlData);\n\n\/\/ Download the html file.\nlet a = document.createElement(&quot;A&quot;);\na.setAttribute(&quot;href&quot;, htmlData);\na.setAttribute(&quot;download&quot;, &quot;filename.html&quot;);\ndocument.body.appendChild(a);\na.click();<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Create &amp; Download a text(.txt) file using javascript.<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\">let text = &#039;You text goes here&#039;;\n\n\/\/ Create the blob url for the file.\ntext = &quot;data:text\/plain,&quot; + encodeURI(text);\n\n\/\/ Download the txt file.\nlet a = document.createElement(&quot;A&quot;);\na.setAttribute(&quot;href&quot;, text);\na.setAttribute(&quot;download&quot;, &quot;filename.txt&quot;);\ndocument.body.appendChild(a);\na.click();<\/pre>\n\n\n\n<p>Thanks for reading this article you can play with code and generate other files like CSV.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will look at how to generate and download excel, html, and text files. 1. Create &amp; Download a excel(.xlsx) file using javascript. Prepare your data in an array. Prepare data for excel. We have completed the preparing data. Now move towards creating a blob URL and download file. 2. Create &amp; <a href=\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":501,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-373222","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create &amp; Download Excel, HTML, and Text files using JavaScript. - Webkul Blog<\/title>\n<meta name=\"description\" content=\"How to generate and download the excel, html , text file using JavaScript. Create the blob URL using multiple mime-type.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create &amp; Download Excel, HTML, and Text files using JavaScript. - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"How to generate and download the excel, html , text file using JavaScript. Create the blob URL using multiple mime-type.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-29T06:32:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-29T06:39:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Vikas Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vikas Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/\"},\"author\":{\"name\":\"Vikas Kumar\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/b8a27a0bc18753ccfc25864afea78bbb\"},\"headline\":\"Create &amp; Download Excel, HTML, and Text files using JavaScript.\",\"datePublished\":\"2023-03-29T06:32:49+00:00\",\"dateModified\":\"2023-03-29T06:39:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/\"},\"wordCount\":118,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/\",\"url\":\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/\",\"name\":\"Create &amp; Download Excel, HTML, and Text files using JavaScript. - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2023-03-29T06:32:49+00:00\",\"dateModified\":\"2023-03-29T06:39:30+00:00\",\"description\":\"How to generate and download the excel, html , text file using JavaScript. Create the blob URL using multiple mime-type.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create &amp; Download Excel, HTML, and Text files using JavaScript.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/b8a27a0bc18753ccfc25864afea78bbb\",\"name\":\"Vikas Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b234cdb5707115f695d3d0d16d69508d111403c6e7ecd52fe7209407b75d17ae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b234cdb5707115f695d3d0d16d69508d111403c6e7ecd52fe7209407b75d17ae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Vikas Kumar\"},\"description\":\"Vikas Kumar is a skilled developer with expertise in React JS, Redux, and Angular JS. He specializes in Custom Plugin Development and PoS App Development, delivering innovative and seamless solutions that enhance user experiences and meet diverse client needs with precision.\",\"url\":\"https:\/\/webkul.com\/blog\/author\/vikas-kumar662\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create &amp; Download Excel, HTML, and Text files using JavaScript. - Webkul Blog","description":"How to generate and download the excel, html , text file using JavaScript. Create the blob URL using multiple mime-type.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Create &amp; Download Excel, HTML, and Text files using JavaScript. - Webkul Blog","og_description":"How to generate and download the excel, html , text file using JavaScript. Create the blob URL using multiple mime-type.","og_url":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2023-03-29T06:32:49+00:00","article_modified_time":"2023-03-29T06:39:30+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Vikas Kumar","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Vikas Kumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/"},"author":{"name":"Vikas Kumar","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/b8a27a0bc18753ccfc25864afea78bbb"},"headline":"Create &amp; Download Excel, HTML, and Text files using JavaScript.","datePublished":"2023-03-29T06:32:49+00:00","dateModified":"2023-03-29T06:39:30+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/"},"wordCount":118,"commentCount":4,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/","url":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/","name":"Create &amp; Download Excel, HTML, and Text files using JavaScript. - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2023-03-29T06:32:49+00:00","dateModified":"2023-03-29T06:39:30+00:00","description":"How to generate and download the excel, html , text file using JavaScript. Create the blob URL using multiple mime-type.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/create-download-excel-html-and-text-files-using-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Create &amp; Download Excel, HTML, and Text files using JavaScript."}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/b8a27a0bc18753ccfc25864afea78bbb","name":"Vikas Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b234cdb5707115f695d3d0d16d69508d111403c6e7ecd52fe7209407b75d17ae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b234cdb5707115f695d3d0d16d69508d111403c6e7ecd52fe7209407b75d17ae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Vikas Kumar"},"description":"Vikas Kumar is a skilled developer with expertise in React JS, Redux, and Angular JS. He specializes in Custom Plugin Development and PoS App Development, delivering innovative and seamless solutions that enhance user experiences and meet diverse client needs with precision.","url":"https:\/\/webkul.com\/blog\/author\/vikas-kumar662\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/373222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/501"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=373222"}],"version-history":[{"count":31,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/373222\/revisions"}],"predecessor-version":[{"id":374424,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/373222\/revisions\/374424"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=373222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=373222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=373222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}