The function really works in Chrome, but I'm having a problem making it work in IE and Firefox and I need to export in Excel to those two browsers for a project. What changes would you require? I already tried using ExecCommand
for IE and it did not work. Does anyone have an idea of what is missing for the function to work?
I based the answer function for the following question:
I have no idea how to do a function that exports a table in HTML and PHP and I do not find it anywhere.
This function I can export the table successfully to Excel but only in Chrome, already in IE and Firefox has no result. You need to have the ability to name the exported file intact as well.
Function code used:
//a classe 'remove' em elementos que não devem aparecer no excel
//(inclusive em inputs type='hidden')
//id da <table> e nome do arquivo a ser gerado
function exportaExcel(id,nome){
var hoje = new Date();
var data = ('0'+hoje.getDate()).slice(-2)+"-"+('0'+(hoje.getMonth()+1)).slice(-2)+"-"+hoje.getFullYear();
var nome_arquivo = nome+"_"+data+".xls"; //nome final do arquivo
var meta = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
var html = $("#"+id).clone();
html.find('.remove').remove();
html.find('a').each(function() {
var txt = $(this).text();
$(this).after(txt).remove();
});
html.find('input, textarea').each(function() {
var txt = $(this).val().replace(/\r\n|\r|\n/g,"<br>");
$(this).after(txt).remove();
});
html.find('select').each(function() {
var txt = $(this).find('option:selected').text();
$(this).after(txt).remove();
});
html.find('br').attr('style', "mso-data-placement:same-cell");
html = "<table>"+html.html()+"</table>";
var uri = 'data:application/vnd.ms-excel,'+encodeURIComponent(meta+html);
var a = $("<a>", {href: uri, download: nome_arquivo});
$(a)[0].click();
}