I\'ve been looking into some jQuery plugins that are capable of doing this. I decided to use the one at http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreads
As @charlietfl said you need the .click() binding inside the $(document).ready because if not the element doesn't exist when you try to bind it.
And this way you dont need to use a plugin
function exportGrid(gridID,filename) {
var html = $('#' + gridID).html();
var a = document.createElement('a');
a.id = 'tempLink';
a.href = 'data:application/vnd.ms-excel,' + html;
a.download = filename + ".xls";
document.body.appendChild(a);
a.click(); // Downloads the excel document
document.getElementById('tempLink').remove();
}
$(document).ready(function() {
$("button").click(function(){
exportGrid("TheGridId","The excel name");
});
});
Here you have a example https://jsfiddle.net/0mzn7uLq/