为特殊字符的Excel创建CSV文件

为特殊字符的Excel创建CSV文件

问题描述:

I have created a simple PHP script as a test. I want to have PHP save a csv from some data. This script works, but when I open it in Excel the special characters are gibberish.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Character Test</title>
</head>

<body>
<ul>
<?php
$spanishDept = array('Swimwear'=>'Trajes de baῆo','Boys Swimwear'=>'Trajes de baῆo','Girls Swimwear'=>'Trajes de baῆo','Kids Shoes'=>'Zapatos de niῆos',"Scarves"=>'Paῆoletas','Children\'s'=>'Niῆos','Coffee Mugs'=>'Tasas para café');

$list = '"Item","Spanish"'."";
foreach($spanishDept AS $english=>$spanish) {
        echo '<li>'.$english.': '.$spanish.'</li>';
        $list .= '"'.$english.'","'.$spanish."\"";
}
$fp = fopen('theEXCELlist.csv',"w+");
fwrite($fp,$list,strlen($list)+100);
?>
</ul>
</body>
</html>

I can open it in NotePad++ or Dreamweaver and all is fine, but in Excel no good

我创建了一个简单的PHP脚本作为测试。 我想让PHP从某些数据中保存csv。 这个脚本有效,但是当我在Excel中打开它时,特殊字符是乱码。 p>

 &lt;!DOCTYPE html PUBLIC“ -  // W3C // DTD XHTML 1.0 Transitional //  EN“”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns =“http://www.w3.org/1999/xhtml”&gt;  
&lt; head&gt; 
&lt; meta http-equiv =“Content-Type”content =“text / html; charset = utf-8”/&gt; 
&lt; title&gt;字符测试&lt; / title&gt; 
&lt; / head&gt  ; 
 
&lt; body&gt; 
&lt; ul&gt; 
&lt;?php 
 $ spanishDept = array('Swimwear'=&gt;'Trajesdebaῆo','Boys Swimwear'=&gt;'Trajesdebaῆo',  'Girls Swimwear'=&gt;'Trajesdebaῆo','Kids Shoes'=&gt;'Zapatosdeniῆos',“Scarves”=&gt;'Paῆoletas','Children \'s'=&gt;'Niῆos','  Coffee Mugs'=&gt;'Tasasparacafé'); 
 
 $ list ='“Item”,“Spanish”'。“
”; 
foreach($ spanishDept AS $ english =&gt; $ spanish){  
 echo'&lt; li&gt;'。$ english。':'。$ spanish。'&lt; / li&gt;'; 
 $ list。='“'。$ english。'”,“'。$ spanish。  “\”
“; 
} 
 $ fp = fopen('theEXCELlist.csv',”w +“); 
fwrite($ fp,$ lis 吨,strlen的($列表)100); 
&GT;吗?
&LT; / UL&GT; 
&LT; / BODY&GT; 
&LT; / HTML&GT; 
 代码>  PRE> 
 
 

我可以在NotePad ++或Dreamweaver中打开它,一切都很好,但在Excel中没有好处 p> div>

Maybe the solution to this problem lies in the "Multibyte String Functions" section of the PHP manual.
In other words, it seems that the PHP output file should be an UTF-16 little endian encoded file (with BOM).
I don't know which version of Excel you're using, but this post may be useful.
Another approach could be to open Excel, insert the special characters, save as CSV and finally check how these characters are encoded in the output file.