<script>
function exportTableToExcel(tableID){
var downloadLink;
var filename = '';
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>
<html>
<table id="dataTableID" Border="1px">
<tr>
<th>From</th>
<th>To</th>
<th>Name</th>
<th>Email</th>
<th>City</th>
</tr>
<tr>
<td>Agra</td>
<td>New Delhi</td>
<td>Aman Kumar</td>
<td>aman@gmail.com</td>
<td>UP</td>
</tr>
<tr>
<td>Mussoorie</td>
<td>Haridwar</td>
<td>Suraj</td>
<td>Suraj@gmail.com</td>
<td>UK</td>
</tr>
<tr>
<td>Jammu</td>
<td>Ghaziabad</td>
<td>Ajay Sharma</td>
<td>Ajay@gmail.com</td>
<td>MP</td>
</tr>
<tr>
<td>Kanpur</td>
<td>Allahabad</td>
<td>Rahul Singh</td>
<td>sam@gmail.com</td>
<td>MP</td>
</tr>
</table>
<br/>
<button onclick="exportTableToExcel('dataTableID')">Download Excel File</button>
</html>