0

I want to create a an XLS file with some info from the database and the data i want to retrieve from the database are all Greek characters.I have this code and when i execute the code i got only some symbols.Anyone knows what i have to change to get Greek characters properly? Edit: The problem is both with the data that i retrieve from the Mysql Database and the headings Here is the code

$sql = "SELECT tmima,sex,surname,address,postcode,phone1,phone2 FROM student"; //the query
$result = $db->prepare($sql);
$result->execute();

$column = $result->columnCount();   
$header = "";

$head = array("Τμήμα","Φ","Επίθετο","Όνομα","Μητρώο"); //headings of the XLS

for($i = 0; $i < $column;$i++ ){
    $header .= $head[$i]."\t";
}

$data = "";

while($row = $result->fetch(PDO::FETCH_ASSOC)/*mysql_fetch_row($rec)*/){
    $line = '';
    foreach($row as $value){

        if((!isset($value)) || ($value == " ")){
            $value = "\t";
        }else{
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }//end of if-else

        $line .= $value;
    }//end of foreach

    $data .= trim( $line ) . "\n";

}//end of while($row = mysql_fetch_row($rec))


$data = str_replace("\r" , "" , $data);

if ($data == ""){
    $data = "\n No Record Found!\n";                       
}

header('Content-Description: File Transfer');
header('Content-Type: application/ms-excel; charset=utf8');
header("Content-Disposition: attachment; filename=Student_data.xls");
//header("Content-Transfer-Encoding: binary ");
header("Expires: 0");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Pragma: public");

print "$header\n$data";
  • 2
    You're not creating an Excel file, just a file with an extension of .xls; but it's actually just a tab-separated value file.... and you should really use PHP's fputcsv() function to create a tab-separated value file rather than writing broken code for it yourself. – Mark Baker Apr 04 '13 at 07:59
  • set the charset to utf8 – aleation Apr 04 '13 at 07:59
  • Don't use UTF-8, use UTF-16 with a BOM header; or use one of the many PHP/Excel libraries to create a real Excel BIFF or OfficeOpenXML file instead – Mark Baker Apr 04 '13 at 08:01
  • IMHO Check your php editor. First of all make sure it is encoding UTF 8 then build a csv file and import it to excel. Or find a php library for xls file building. – Volkan Apr 04 '13 at 08:02

1 Answers1

1

no the data i get from the Mysql qquery is with greek characters

Make sure your database connection is UTF-8 encoded. See UTF-8 all the way through on how to do that.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088