1
I wonder if it is possible to create a function in PHP and/or Javascript that allows to export the contents of a table in HTML (table inside a div) straight to an Excel file.
I’m trying to take advantage of a function created from the site of a client of mine, but it didn’t work out very well. So I think I need to do something new.
I thought of something like this: having a table with id = 'tableOne' and having on my page a button with id = 'testExcel'. When you click this button, it goes to my javascript function:
$('#testExcel').click(function(){
    var conteudo_div = $('#tableOne').text();
    $.ajax({
        async: false,
        cache: false,
        data: ({
            conteudo_div: $('#tableOne').text()
        }),
        url: '[:raiz]cadAdmin/testExcel',
        dataType: 'json',
        success: function(data){
            alert(conteudo_div);
        }
    })
});
And then the testExcel function of the URL is in a Controller file on my page:
public function testExcel(){
    $nome_arquivo = "Listagem de Investidores";
    header("Content-type: application/vnd.ms-excel");
    header("Content-type: application/force-download");
    header("Content-Disposition: attachment; filename=$nome_arquivo.xls");
    header("Pragma: no-cache");
    $html = $_REQUEST['conteudo_div'];
    echo $html;
} 
Only, for now, it didn’t work. Any idea?
Good question...
– Ricardo