doubiaokai4998 2012-08-06 16:18
浏览 79
已采纳

为特殊字符的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

  • 写回答

1条回答 默认 最新

  • dtpxi88884 2012-08-06 20:19
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器