Fórum EXPORTAR RELATÓRIO EXCEL PHP JQUERY #582257
08/06/2017
0
Boa tarde amigos,
Preciso exportar um relatório em formato excel de minha base de dados ao clicar em um button.
Os código são os seguintes:
Código 1:
Código para gerar a tabela:
Porém não está funcionando, a tabela apenas é escrita em HTML e o download não é disparado, fica como na imagem do link:
PAGINA APOS CLICAR NO BUTTON
Preciso exportar um relatório em formato excel de minha base de dados ao clicar em um button.
Os código são os seguintes:
Código 1:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#GeraExcel").click(function(){
var areaEscolhida = $("#areaEscolhida").val();
var cargoEscolhido = $("#cargoEscolhido").val();
console.log(areaEscolhida);
console.log(cargoEscolhido);
$.ajax({
type: 'POST',
url: './exportExcel1.php',
headers: {
'Cache-Control': 'no-cache, must-revalidate',
'Pragma': 'no-cache',
'Content-Type': 'application/x-msexcel',
'Content-Disposition': 'attachment; filename=\\"{$arquivo}\\"'
},
data: {
areaEscolhida: areaEscolhida,
cargoEscolhido: cargoEscolhido
},
success: function (data){
console.log(data);
alert('Sucesso!');
var win=window.open();
with(win.document)
{
open();
write(data);
close();
}
},
error: function (data) {
console.log(data);
alert('Houve um erro, tente novamente mais tarde!');
}
})
});
});
</script>
Código para gerar a tabela:
// Criamos uma tabela HTML com o formato da planilha para excel
$tabela .= '<meta http-equiv="pragma" content="no-cache" />';
$tabela .= '<meta http-equiv="cache-control" content="no-cache" />';
$tabela .= '<meta http-equiv="content-type" content="application/x-msexcel" />';
$tabela .= '<meta http-equiv="content-disposition" content="attachment; filename=\\"{$arquivo}\\"" />';
$tabela .= '<table border="1">';
$tabela .= '<tr>';
$tabela .= '<td colspan="46"><b>Formulario - Levantamento Funcional - Respostas</b></td>';
$tabela .= '</tr>';
$tabela .= '<tr style="text-align: center;">';
$tabela .= '<td rowspan="2"><b>01.01.00-NOM</b></td>';
$tabela .= '<td rowspan="2"><b>01.02.00-MTR</b></td>';
$tabela .= '<td rowspan="2"><b>01.03.00-CRG</b></td>';
$tabela .= '<td rowspan="2"><b>01.03.00-ADM</b></td>';
$tabela .= '<td rowspan="2"><b>01.04.00-LTC</b></td>';
$tabela .= '<td rowspan="2"><b>02.01.00-SMD</b></td>';
$tabela .= '<td rowspan="2"><b>02.02.00-SIM</b></td>';
$tabela .= '<td rowspan="2"><b>02.03.00-DMD</b></td>';
$tabela .= '<td rowspan="2"><b>02.04.00-SBM</b></td>';
$tabela .= '<td colspan="2"><b>02.05.01-SBD</b></td>';
$tabela .= '<td rowspan="2"><b>03.01.01-ATV</b></td>';
$tabela .= '<td rowspan="2"><b>03.02.01-SMN </b></td>';
$tabela .= '<td rowspan="2"><b>03.03.01-QZN</b></td>';
$tabela .= '<td rowspan="2"><b>03.04.01-MSL</b></td>';
CONTINUÇÃO DO CÓDIGO PARA GERAR AS COLUNAS / LINHAS.
...
FINAL DO CÓDIGO QUE IRIA FORÇAR O DOWNLOAD DA TABELA:
$tabela .= '<td>'.utf8_decode($dados['conhecimentoLegislacao']).'</td>';
$tabela .= '<td>'.utf8_decode($dados['conhecimentoOutros']).'</td>';
$tabela .= '<td>'.utf8_decode($dados['reqEscolaridade']).'</td>';
$tabela .= '<td>'.utf8_decode($dados['reqExperiencia']).'</td>';
$tabela .= '<td>'.utf8_decode($dados['reqEspecializacao']).'</td>';
$tabela .= '<td>'.utf8_decode($dados['reqComportamento']).'</td>';
$tabela .= '<td>'.utf8_decode($dados['reqOutros']).'</td>';
$tabela .= '<td>'.utf8_decode($dados['sugestoesTrabalho']).'</td>';
$tabela .= '<td>'.utf8_decode($dados['observacoesGerais']).'</td>';
$tabela .= '</tr>';
}
$tabela .= '</table>';
// Força o Download do Arquivo Gerado
header ('Cache-Control: no-cache, must-revalidate');
header ('Pragma: no-cache');
header('Content-Type: application/x-msexcel');
header ("Content-Disposition: attachment; filename=\\"{$arquivo}\\"");
echo $tabela;
?>
Porém não está funcionando, a tabela apenas é escrita em HTML e o download não é disparado, fica como na imagem do link:
PAGINA APOS CLICAR NO BUTTON
Salute Especializadas
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)