I would like to know if it is possible to create a function in PHP and / or Javascript that allows exporting the contents of an HTML table (table inside a div) directly into an Excel file.
I'm trying to take advantage of a function created from the site of a client of mine, but it did not work 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 I click this button, it goes into 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 URL function 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 did not work. Any ideas?