Export Table in EXCEL with PHP

Asked

Viewed 941 times

0

I am exporting my data to excel format, but when I export the accents do not return correctly, this is my script:

<?php include_once "bancodedados.php"; $table="smt_cadastro_sociedades";
$select = "SELECT * FROM ".$table;                
$export = mysql_query($select);
$fields = mysql_num_fields($export); 
for ($i = 0; $i < $fields; $i++) {
    $header .= mysql_field_name($export, $i) . "\t"; 
}
while($row = mysql_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {                                            
        if ((!isset($value)) OR ($value == "")) {
            $value = "\t";
        } else {
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim($line)."\n";
}
$data = str_replace("\r","",$data); 
if ($data == "") {
    $data = "\n(0) Records Found!\n";                        
}
else{
    $hoje=date("j_m_Y");              
    header("Content-type: application/x-msdownload");
    header("Content-Disposition: attachment; filename=".$hoje.".xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\n$data";  
}?>

Return example: inserir a descrição da imagem aqui

  • 1

    In value puts utf8_encode($value).

  • On this line foreach($Row as $value) {

  • 1

    After that $value = '"' . $value . '"' . "\t"; puts this command $value = utf8_encode($value).

  • So: } Else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . " t"; $value = utf8_encode($value); }

  • 1

    Excel only correctly displays characters in ISO-8859-1. You have to set the data output header and its contents in this charset. use the hint and the functions there to display the content correctly. http://answall.com/questions/91555/por-que-a-string-do-php-as-vezes-retorna-no-lugar-de-algumas-letras-accented/91628#91628

No answers

Browser other questions tagged

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