{"id":126804,"date":"2022-07-25T14:55:03","date_gmt":"2022-07-25T14:55:03","guid":{"rendered":"https:\/\/phppot.com\/?p=18737"},"modified":"2022-07-25T14:55:03","modified_gmt":"2022-07-25T14:55:03","slug":"convert-html-table-to-excel-using-javascript","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/07\/25\/convert-html-table-to-excel-using-javascript\/","title":{"rendered":"Convert HTML Table to Excel using JavaScript"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.0697674418605\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on July 25th, 2022.<\/div>\n<p>Converting a HTML table to an excel file is a standard requirement of reporting websites. It will be simple and easier if the conversion is taking place on the client side.<\/p>\n<p>There are many client-side and server-side plugins to perform the excel export. For example, PHPSpreadSheet allows writing data into excel and exporting.<\/p>\n<p>This article will give different options and approaches to achieving the HTML table to excel conversion with JavaScript.<\/p>\n<p>This simple example includes a few lines of JavaScript that build the template for excel export.<\/p>\n<p>It uses the following <span>steps to convert<\/span> the exported excel report of tabular data.<\/p>\n<ol>\n<li>Defines HTML template structure with required meta.<\/li>\n<li>Include the table\u2019s inner HTML into the template.<\/li>\n<li>Marks the location by specifying the protocol to download the file via browser.<\/li>\n<li><a href=\"https:\/\/phppot.com\/javascript\/javascript-redirect\/\">Redirect via JavaScript<\/a> to point to the location with the encoded excel content.<\/li>\n<\/ol>\n<div class=\"post-section-highlight\" readability=\"38\">\n<h2>Quick example<\/h2>\n<p>A JavaScript handler to set export template and push HTML table data.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-javascript\">function exportToExcel() { var location = 'data:application\/vnd.ms-excel;base64,'; var excelTemplate = '&lt;html&gt; ' + '&lt;head&gt; ' + '&lt;meta http-equiv=\"content-type\" content=\"text\/plain; charset=UTF-8\"\/&gt; ' + '&lt;\/head&gt; ' + '&lt;body&gt; ' + document.getElementById(\"table-conatainer\").innerHTML + '&lt;\/body&gt; ' + '&lt;\/html&gt;' window.location.href = location + window.btoa(excelTemplate);\n}\n<\/code><\/pre>\n<\/div>\n<p><a class=\"demo\" href=\"https:\/\/phppot.com\/demo\/html-table-excel-javascript\/\">View demo<\/a><\/p>\n<p>The below HTML table code displays a product listing and it is static. Refer <a href=\"https:\/\/phppot.com\/javascript\/jspdf-autotable\/\">jsPDF AutoTables examples<\/a> to render a dynamic table by loading row by row.<\/p>\n<p>The button below this table triggers the export to excel process on the click event. The <em>exportToExcel()<\/em> function handles the event and proceeds the client-side export.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-html\">&lt;div id=\"table-conatainer\"&gt; &lt;table class=\"striped\"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;S.No&lt;\/th&gt; &lt;th&gt;Product Name&lt;\/th&gt; &lt;th&gt;Price&lt;\/th&gt; &lt;th&gt;Model&lt;\/th&gt; &lt;\/tr&gt; &lt;\/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;1q&lt;\/td&gt; &lt;td&gt;GIZMORE Multimedia Speaker with Remote Control, Black&lt;\/td&gt; &lt;td&gt;2300&lt;\/td&gt; &lt;td&gt;2020&lt;\/td&gt; &lt;\/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;\/td&gt; &lt;td&gt;Black Google Nest Mini&lt;\/td&gt; &lt;td&gt;3400&lt;\/td&gt; &lt;td&gt;2021&lt;\/td&gt; &lt;\/tr&gt; &lt;\/tbody&gt; &lt;\/table&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-18767\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/07\/html-table-excel-javascript-550x318.jpg\" alt=\"html table excel javascript\" width=\"550\" height=\"318\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/07\/html-table-excel-javascript-550x318.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/07\/html-table-excel-javascript-300x173.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/07\/html-table-excel-javascript-768x444.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/07\/html-table-excel-javascript.jpg 800w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<p>Let us see more options available for converting HTML table data into an excel file.<\/p>\n<h2>Option 2: Export as CSV to view in excel<\/h2>\n<p>This example is for parsing the HTML table content via JavaScript. It follows the below steps to export an HTML table to excel as CSV.<\/p>\n<ol>\n<li>Accessing HTML table element object.<\/li>\n<li>Convert the object into an array of row data.<\/li>\n<li>Iterate row data array to prepare <a href=\"https:\/\/phppot.com\/php\/php-csv-file-read\/\">comma-separated values<\/a> of records.<\/li>\n<li>Set the protocol and content type to export the CSV data prepared from the table.<\/li>\n<\/ol>\n<p>The below JavaScript function implements the above steps to export table data on the client side.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-javascript\">function exportCSVExcel() { var tableElement = document.getElementById(\"table-product-list\"); var sourceData = \"data:text\/csv;charset=utf-8,\"; var i = 0; while (row = tableElement.rows[i]) { sourceData += ([ row.cells[0].innerText, row.cells[1].innerText, row.cells[2].innerText, row.cells[3].innerText ]).join(\",\") + \"\\r\\n\"; i++; } window.location.href = encodeURI(sourceData);\n}\n<\/code><\/pre>\n<h2>Option 3 \u2013 Export HTML table to excel using jQuery-based plugin<\/h2>\n<p>The jQuery table2excel plugin is a popular solution to arrive HTML to excel export.<\/p>\n<p>It has many features with the export core functionalities. Those are,<\/p>\n<ol>\n<li>Excludes\/includes HTML tags like inputs, links, or images that are in the source table HTML.<\/li>\n<li>Excludes some table components with the reference of that particular element\u2019s class or id selectors.<\/li>\n<li>Provides properties to set file name with which the exported data will be downloaded to the browser.<\/li>\n<\/ol>\n<p>Include the jQuery and the table2excel plugin file in the&nbsp;<em>&lt;head&gt;<\/em> section as below.<\/p>\n<p>There are alternative methods to install the table2excel plugin. Its <a href=\"https:\/\/github.com\/rainabba\/jquery-table2excel\" target=\"_blank\" rel=\"noopener\">Github page provides documentation<\/a> of installation and usage methodologies.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-html\">&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.2.4\/jquery.min.js\"&gt;&lt;\/script&gt;\n&lt;script src=\"https:\/\/cdn.rawgit.com\/rainabba\/jquery-table2excel\/1.1.0\/dist\/jquery.table2excel.min.js\"&gt;&lt;\/script&gt;\n<\/code><\/pre>\n<p>Then, initiate the plugin class by pointing to the source HTML table element.<\/p>\n<p>The below JavaScript does the initiation. During instantiation, it sets the options array to specify a file name, extension and applicable flags.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-javascript\">function exportCSVExcel() { $('#table-product-list').table2excel({ exclude: \".no-export\", filename: \"download.xls\", fileext: \".xls\", exclude_links: true, exclude_inputs: true });\n}\n<\/code><\/pre>\n<p>This example uses the same HTML table source for the export operation. The difference is that the source table content marks some of the rows with <em>no-export<\/em> class.<\/p>\n<p>This class is configured in the above script with&nbsp;<em>exclude<\/em> property of this plugin class.<\/p>\n<h2>Conclusion:<\/h2>\n<p>So, we have seen three simple implementations of adding HTML table to excel feature. Though the export feature is vital, we must have a component that provides a seamless outcome.<\/p>\n<p>I hope, the above solution can be a good base for creating such components. It is adaptable for sure to dock more features to have an efficient excel report generation tool.<\/p>\n<p><a class=\"demo\" href=\"https:\/\/phppot.com\/demo\/html-table-excel-javascript\/\">View demo<\/a><a class=\"download\" href=\"https:\/\/phppot.com\/downloads\/html-table-excel-javascript.zip\">Download<\/a><\/p>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/javascript\/convert-html-table-excel-javascript\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on July 25th, 2022. Converting a HTML table to an excel file is a standard requirement of reporting websites. It will be simple and easier if the conversion is taking place on the client side. There are many client-side and server-side plugins to perform the excel export. For example, PHPSpreadSheet allows [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":126805,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-126804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=126804"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126804\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/126805"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}