Export query to EXCEL in PHP error UTF-8

Asked

Viewed 230 times

0

When I export the data the UTF-8 is not acting, how can I solve this?

// Formatação do header do arquivo
header ("Expires: Mon, 18 Nov 1985 18: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=Dados.xls");

header ("Content-Type: text/html; charset=UTF-8");
echo "\xEF\xBB\xBF";

?>
<table>
    <thead>
        <tr>
            <th>Fazenda</th>
            <th>Talhão</th>
            <th>Data</th>
            <th>Deficit (mm)</th>
            <th>Irrigação (mm)</th>
            <th>Irrigação (h)</th>
            <th>Velocidade (%)</th>
            <th>Pluviometria (mm)</th>
            <th>Temp Min (°C)</th>
            <th>Temp Med (°C)</th>
            <th>Temp Max (°C)</th>

        </tr>
    </thead>

    <tbody>
        <?php
            while ($Dado = $conexao->busca($Dados))
            {
                ?>
                <tr>
                    <td><?=$Dado['Nome_Fazenda']?></td>
                    <td><?=$Dado['Nome_Parcela']?></td>
                    <td><?=$Dado['Data_Irr']?></td>
                    <td><?=$Dado['Deficit']?></td>
                    <td><?=$Dado['IrrMM']?></td>
                    <td><?=$Dado['IrrHS']?></td>
                    <td><?=$Dado['Velocidade']?></td>
                    <td><?=$Dado['Pluviometria']?></td>
                    <td><?=$Dado['Temp_Min']?></td>
                    <td><?=$Dado['Temp_Med']?></td>
                    <td><?=$Dado['Temp_Max']?></td>
                </tr>
                <?php
            }
        ?>
    </tbody>

Planilha

  • the problem occurs in the elements that come from the comic or all, including the ones that you typed by hand?

  • in all, I’ll put an image for you to see how it looks, still comes a symbol at the beginning.

  • @Weessmith made the change.

  • have something before these headers?

  • Yes, a query to search in the $data variable.

  • the headers have to be the first of everything, puts them isolated at the top of the page just below <php?

Show 1 more comment

1 answer

0


The header()s should always come at the top of the page before anything:

<?php
    header ("Expires: Mon, 18 Nov 1985 18: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=Dados.xls");
    header ("Content-Type: text/html; charset=UTF-8");
  • I made the change, but it still didn’t work.

  • just leave the header("charset=UTF-8") and take the others to see

  • On the page appears all right, when it is exporting to Excel that is not working

  • give a look here https://stackoverflow.com/questions/4164719/utf-8-encoding-xml-in-php

  • 1

    All right, I’ll check.

  • 1

    Got it buddy, thanks!

Show 1 more comment

Browser other questions tagged

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