Function to export table in html and php to Excel

Asked

Viewed 1,090 times

1

The function really works in Chrome, but I have a problem to make it work in IE and Firefox and I need to export in Excel to these two browsers, for one project. What changes would be needed? I’ve tried using ExecCommand for the IE and came up with nothing. Someone has an idea of what is missing for the full functioning of the function?

Based on the answer function for the following question:

Export from html table to excel

I have no idea how to make a function that exports a table in HTML and PHP and I can’t find it anywhere.

This function I can successfully export the table to Excel but only in Chrome, already in IE and Firefox has no result. And I need to have the feature to name the exported file intact as well.

Code of function 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();
}
  • You can [Edit] the question and add the code you are using?

  • Edited. This is just the function I’m using for export;

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.