我已經使用下面的代碼使用php導出excel文件。對於英文文本,我沒有發現任何問題。但俄羅斯人物我發現一些問題。我附上我的代碼。PHP導出excel unicode(俄羅斯字符)問題
mysql_connect("localhost","USERNAME","PASSWORD");
mysql_select_db("DATABASE");
mysql_query("SET NAMES 'UTF8'");
// Get data records from table.
$result=mysql_query("select * from usertbl order by user_id asc");
// Functions for export to excel.
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: application/vnd.ms-excel;charset:UTF-8");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=orderlist.xls ");
header("Content-Transfer-Encoding: binary ");
xlsBOF();
/*
Make a top line on your excel sheet at line 1 (starting at 0).
The first number is the row number and the second number is the column, both are
start at '0'*/
xlsWriteLabel(0,0,"List of car company.");
// Make column labels. (at line 3)
xlsWriteLabel(2,0,"No.");
xlsWriteLabel(2,1,"Nickname");
$xlsRow = 3;
// Put data records from mysql by while loop.
while($row=mysql_fetch_array($result)){
xlsWriteNumber($xlsRow,0,$row['user_id']);
//$row['nickname']= iconv('UTF-8', 'SJIS', $row['NAME']);
//$row['nickname'] = mb_convert_encoding($row['NAME'], 'UTF-16LE', 'UTF-8');
xlsWriteLabel($xlsRow,1,$row['NAME']);
$xlsRow++;
}
xlsEOF();
exit();
你可以嘗試使用http://phpexcel.codeplex.com/ – Petah 2011-06-02 05:40:00
當我有問題,在俄羅斯我發送電子郵件發現mysqli_set_charset($ connector,'utf8');幫助(其中連接器 - 是你的db連接變量設置爲$ connector = @mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) - 你必須首先定義這些東西)再次檢查php文件本身是否以UTF-8編碼,無BOM並且所有表中的所有字段都是UTF-8。這裏是關於mysqli_set_charset更多信息:http://php.net/manual/en/mysqli.set-charset.php – 2011-06-02 05:53:13