我想創建一個XLS文件,其中包含來自數據庫的一些信息,我想從數據庫中檢索的數據都是希臘字符。我有這個代碼,當我執行代碼時只有一些符號。任何人都知道我必須改變才能正確地得到希臘字符? 編輯:問題是雙方的數據,我從MySQL數據庫和標題 這裏檢索是代碼創建XLS文件(希臘字符)
$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";
您並未創建Excel文件,只是一個擴展名爲.xls的文件;但它實際上只是一個製表符分隔的值文件....並且您應該真正使用PHP的fputcsv()函數來創建制表符分隔的值文件,而不是自己爲其編寫破碎的代碼。 – 2013-04-04 07:59:43
將字符集設置爲utf8 – aleation 2013-04-04 07:59:45
不要使用UTF-8,請將UTF-16與BOM標題一起使用;或者使用其中一個PHP/Excel庫創建一個真正的Excel BIFF或OfficeOpenXML文件,而不是 – 2013-04-04 08:01:14