0

A partir dese tutorial no youtube, estou tentando gerar um Excel do meu BD MySql através de uma query do PHP. Porém não é feito o download da planilha e o resultado gera 5 warnings de header com a planilha printada na tela.

Ao pesquisar sobre o assunto, foi sugerido que mudasse para UTF8-BOM, porém também não deu certo.

Segue código e resultado.

gerarexcel.php

<?php
    session_start();
    include_once('conecta.php');
?>
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <title>Gerar Excel</title>
    </head>
    <body>
        <?php
        // Definimos o nome do arquivo que será exportado
        $arquivo = 'relatoriotivit.xls';

        // Criamos uma tabela HTML com o formato da planilha
        $html = '';
        $html .= '<table border="1">';
        $html .= '<tr>';
        $html .= '<td colspan="8"><center>Relatório - Mês de Novembro</center></tr>';
        $html .= '</tr>';

        $html .= '<tr>';
        $html .= '<td><b>Datas</b></td>';
        $html .= '<td><b>Modulo</b></td>';
        $html .= '<td><b>Assunto/Script</b></td>';
        $html .= '<td><b>Descrição/Alteração</b></td>';
        $html .= '<td><b>Script/Informação</b></td>';
        $html .= '<td><b>Responsável</b></td>';
        $html .= '<td><b>Tipo</b></td>';
        $html .= '<td><b>Origem</b></td>';
        $html .= '</tr>';

        //Selecionar todos os itens da tabela 
        $result_msg_contatos = "SELECT * FROM formulario";
        $resultado_msg_contatos = mysqli_query($conn , $result_msg_contatos);

        while($row_msg_contatos = mysqli_fetch_assoc($resultado_msg_contatos)){
            $html .= '<tr>';
            $html .= '<td>'.$row_msg_contatos["Datas"].'</td>';
            $html .= '<td>'.$row_msg_contatos["Modulo"].'</td>';
            $html .= '<td>'.$row_msg_contatos["AssuntoScript"].'</td>';
            $html .= '<td>'.$row_msg_contatos["DescricaoAlteracao"].'</td>';
            $html .= '<td>'.$row_msg_contatos["ScriptInformacao"].'</td>';
            $html .= '<td>'.$row_msg_contatos["Responsavel"].'</td>';
            $html .= '<td>'.$row_msg_contatos["Tipo"].'</td>';
            $html .= '<td>'.$row_msg_contatos["Origem"].'</td>';
            $html .= '</tr>';
            ;
        }


        // Configurações header para forçar o download
        header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
        header ("Cache-Control: no-cache, must-revalidate");
        header ("Pragma: no-cache");
        header ("Content-type: application/x-msexcel");
        header ("Content-Disposition: attachment; filename=\"{$arquivo}\"" );
        header ("Content-Description: PHP Generated Data" );


        // Envia o conteúdo do arquivo
        echo $html;
        exit; 

        ?>
        </body>
        </html>

RESULTADO EM TELA:

Warning: Cannot modify header information - headers already sent by (output started at /home/sitedoda/public_html/trabalho/FormEnvioAnexo/conecta.php:7) in /home/sitedoda/public_html/trabalho/FormEnvioAnexo/gerarexcel.php on line 54

Warning: Cannot modify header information - headers already sent by (output started at /home/sitedoda/public_html/trabalho/FormEnvioAnexo/conecta.php:7) in /home/sitedoda/public_html/trabalho/FormEnvioAnexo/gerarexcel.php on line 55

Warning: Cannot modify header information - headers already sent by (output started at /home/sitedoda/public_html/trabalho/FormEnvioAnexo/conecta.php:7) in /home/sitedoda/public_html/trabalho/FormEnvioAnexo/gerarexcel.php on line 56

Warning: Cannot modify header information - headers already sent by (output started at /home/sitedoda/public_html/trabalho/FormEnvioAnexo/conecta.php:7) in /home/sitedoda/public_html/trabalho/FormEnvioAnexo/gerarexcel.php on line 57

Warning: Cannot modify header information - headers already sent by (output started at /home/sitedoda/public_html/trabalho/FormEnvioAnexo/conecta.php:7) in /home/sitedoda/public_html/trabalho/FormEnvioAnexo/gerarexcel.php on line 58

Warning: Cannot modify header information - headers already sent by (output started at /home/sitedoda/public_html/trabalho/FormEnvioAnexo/conecta.php:7) in /home/sitedoda/public_html/trabalho/FormEnvioAnexo/gerarexcel.php on line 59

Warning: Cannot modify header information - headers already sent by (output started at /home/sitedoda/public_html/trabalho/FormEnvioAnexo/conecta.php:7) in /home/sitedoda/public_html/trabalho/FormEnvioAnexo/gerarexcel.php on line 

(e a tabela, conforme este link)

2

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .