duangong0690
2012-02-16 10:05
浏览 51
已采纳

CSV传输到Excel的编码错误

Well the excel 2003 can open the csv files and if the the delimiter is tab it can automatically transform data into correct columns.

Well the problem is with the characters which aren't the ascii ones. The libreoffice opens the dialog for selecting charset but the excel 2003 does not and imports the characters into wrong charset.

<?php
$filename ="excelreport.xls";

$contents1 = "Name \t Location (where from) \t Age \t 
";
$contents = "testdata1 \t testdata2 \t testdata3 \t 
";
$problematicData = array('ä', 'ö', 'õ', 'ü');

header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);

echo $contents1;
echo $contents;
echo implode(" \t ", $problematicData);

// EDIT Probably i found a fix added this header('Content-type: application/ms-excel; charset=utf-8; encoding=utf-8'); But i haven't tested it with the excel 2003.

图片转代码服务由CSDN问答提供 功能建议

excel 2003可以打开csv文件,如果分隔符是tab,它可以自动将数据转换为正确的列 。

问题在于不是ascii的字符。 libreoffice打开用于选择charset的对话框,但excel 2003没有,并将字符导入错误的字符集。

 &lt;?php 
 $ filename =“excelreport.xls”  ; 
 
 $ contents1 =“名称\ t位置(从哪里)\ t年龄\ t 
”; 
 $ contents =“testdata1 \ t testdata2 \ t testdata3 \ t 
”; 
 $ problematicData =  array('ä','ö','õ','ü'); 
 
header('Content-type:application / ms-excel'); 
header('Content-Disposition:attachment; filename ='  。$ filename); 
 
echo $ contents1; 
echo $ contents; 
echo implode(“\ t”,$ problematicData); 
   
 
 

//编辑 可能我发现修复程序添加了此标题('Content-type:application / ms-excel; charset = utf-8; encoding = utf-8'); 但我还没有用excel 2003测试它。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxunchen3498 2012-04-15 05:28
    已采纳

    Tried using commas instead? PHP has a fputcsv function that can handle that easily

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题