LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Export HTML Table Data To Excel (.xls) File Using Vanilla JavaScript

Export HTML Table Data To Excel (.xls) File Using Vanilla JavaScript

Export-HTML-Table-Data-To-Excel-(.xls)-File-Using-Vanilla-JavaScript
Exporting data to Excel is very useful on the data list for nearly every web application. The export feature helps to download the data list as a file format for offline use. Excel format is ideal for exporting data in a file. Mostly the server-side method is used for exporting data to excel using PHP. But if you want a client-side solution to export table data to excel, it can be easily done using JavaScript.

The client-side export functionality makes the web application user-friendly. Using JavaScript, the HTML table data can be easily exported without page refresh. In this tutorial, we will show you how to export HTML table data to excel using JavaScript. The JavaScript export functionality can be used in the member list, product list, or other lists to download the data list in excel file format.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Table of Contents

Recommended For You:
Simple And Rich Toast-Style Web Notifications In Vanilla JavaScript – VanillaToasts

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add Export HTML Table Data To Excel (.xls) File Using Vanilla JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

HTML:

<table id="tblData">
<tr>
<th>Name</th>
<th>Email</th>
<th>Country</th>
</tr>
<tr>
<td>John Doe</td>
<td>john@gmail.com</td>
<td>USA</td>
</tr>
<tr>
<td>Michael Addison</td>
<td>michael@gmail.com</td>
<td>UK</td>
</tr>
<tr>
<td>Sam Farmer</td>
<td>sam@gmail.com</td>
<td>France</td>
</tr>
</table>
<button onclick="exportTableToExcel('tblData', 'members-data')">Export Table Data To Excel File</button>

JavaScript:

<script type="text/javascript">
function exportTableToExcel(tableID, filename = ''){
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

// Specify file name
filename = filename?filename+'.xls':'excel_data.xls';

// Create download link element
downloadLink = document.createElement("a");

document.body.appendChild(downloadLink);

if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{
// Create a link to the file
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;

// Setting the file name
downloadLink.download = filename;

//triggering the function
downloadLink.click();
}
}
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.
The exportTableToExcel() function converts HTML table data to excel and download it as an XLS file (.xls).

tableID – Required. Specify the HTML table ID to export data from.

filename – Optional. Specify the file name to download excel data.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Recommended For You:
Simple Custom Select Dropdown Example Using Pure JavaScript

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *