简体   繁体   English

使用php将MySQL表导出到excel。 编码问题

[英]Export MySQL table using php into excel. Problems with encoding

I try to export one of MySQL table into excel file but polish letters aren't displaying correctly It is displayed: Bre¶ć, Białoru¶ but should: Brześć, Białoruś 我尝试将MySQL表之一导出到excel文件中,但波兰字母未正确显示它显示:Bre¶ć,Białoru¶但应:Brześć,Białoruś

How can I fix this code? 如何解决此代码? It is really anoying that polish letters aren't displayed. 没有显示波兰字母真的很烦。 Where could be a problem? 哪里可能有问题?

<?php

 $DB_Server = "localhost";      //your MySQL Server 
$DB_Username = "******";                 //your MySQL User Name 
$DB_Password = "*****";             //your MySQL Password 
$DB_DBName = "*******";             //your MySQL Database Name 
$DB_TBLName = "********";               //your MySQL Table Name  

 $filename = "excelfilename";         //File Name

 //create MySQL connection
 $sql = "Select * from $DB_TBLName";
 $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
 or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
 //select database
 $Db = @mysql_select_db($DB_DBName, $Connect)
 or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
 //execute query
 $result = @mysql_query($sql,$Connect)
 or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
 $file_ending = "xls";

//header info for browser
 header("Content-Type: application/xls");
 header("Content-Disposition: attachment; filename=$filename.xls");
 header("Pragma: no-cache");
 header("Expires: 0");

/*******Start of Formatting for Excel*******/
 //define separator (defines columns in excel & tabs in word)
 $sep = "\t"; //tabbed character

//start of printing column names as names of MySQL fields
 for ($i = 0; $i < mysql_num_fields($result); $i++) {
 echo mysql_field_name($result,$i) . "\t";
 }
 print("\n");
 //end of printing column names

//start while loop to get data
 while($row = mysql_fetch_row($result))
 {
 $schema_insert = "";
 for($j=0; $j<mysql_num_fields($result);$j++)
 {
 if(!isset($row[$j]))
 $schema_insert .= "NULL".$sep;
 elseif ($row[$j] != "")
 $schema_insert .= "$row[$j]".$sep;
 else
 $schema_insert .= "".$sep;
 }
 $schema_insert = str_replace($sep."$", "", $schema_insert);
 $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
 $schema_insert .= "\t";
 print(trim($schema_insert));
 print "\n";
 }
 ?>

You have to write what is charset of you database. 您必须编写什么是数据库的字符集。 If it is UTF8 try to execute query "SET NAMES utf8" after connection is established ; 如果是UTF8,则在建立连接后尝试执行查询“ SET NAMES utf8”;

And be shure that encoding of you page is UTF8. 并且请确保您页面的编码是UTF8。 You can check encoding in Notepad++ utility 您可以在Notepad ++实用程序中检查编码

Problem is your output is in ISO-8859-2 but excel expects Windows-1250 which has few characters shuffled around for Windows-1252 compatibility. 问题是您的输出是在ISO-8859-2但是excel希望Windows-1250字符很少能与Windows-1252兼容。

You can either convert your output to Windows-1250 您可以将输出转换为Windows-1250

echo mb_convert_encoding($str, 'Windows-1250', 'ISO-8859-2');

Or convert to utf8: 或转换为utf8:

add utf8 BOM after headers and first thing as file content: 在标头和第一件事后添加utf8 BOM作为文件内容:

echo "\xEF\xBB\xBF";

and convert data to utf8 并将数据转换为utf8

echo mb_convert_encoding($str, 'UTF-8', 'ISO-8859-2');

If you have older excel that does not support utf8 you are out of luck. 如果您有不支持utf8的旧版excel,那么您会很不幸。


By default excel assumes windows default locale which is different for people from different locations ie person in US will likely see your text as "Brze¶æ, Bia³oru¶" instead 默认情况下,excel假定Windows的默认区域设置对于来自不同位置的人而言是不同的,即,在美国的人可能会看到您的文本为“Brze¶æ,Bia³oru¶”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM