When trying to generate in Excel by PHP, appear Warnings and spreadsheet is printed in the browser

Asked

Viewed 93 times

0

Since youtube tutorial, I am trying to generate an Excel of my BD Mysql through a PHP query. But the sheet is not downloaded and the result generates 5 warnings of header with the sheet printed on the screen.

When researching on the subject, it was suggested to switch to UTF8-BOM, but it also did not work.

Follow code and result.

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>

SCREEN RESULT:

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 

(and the table as this link)

No answers

Browser other questions tagged

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