Excel csv导出到具有fgetcsv的php文件 [英] Excel csv export into a php file with fgetcsv

查看:169
本文介绍了Excel csv导出到具有fgetcsv的php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用excel 2010专业加创建一个excel文件。
后来我试图将其导出为UTF-8 .csv文件。
我这样做通过保存为CSV(符号分隔... sry我不知道确切的字词有
,但我没有英文版,我害怕它翻译不同于1:1
)。
我点击tools-> weboptions并选择unicode(UTF-8)作为编码。
示例.csv如下:

I'm using excel 2010 professional plus to create an excel file. Later on I'm trying to export it as a UTF-8 .csv file. I do this by saving it as CSV (symbol separated.....sry I know not the exact wording there but I don't have the english version and I fear it is translated differently than 1:1). There I click on tools->weboptions and select unicode (UTF-8) as encoding. The example .csv is as follows:

ID;englishName;germanName
1;Austria;Österreich

到目前为止很好,但如果我现在用我的PHP代码打开文件:

So far so good, but if I open the file now with my php code:

 header('Content-Type: text/html; charset=UTF-8');
 iconv_set_encoding("internal_encoding", "UTF-8");
 iconv_set_encoding("output_encoding", "UTF-8");
 setlocale(LC_ALL, 'de_DE.utf8');
 $fp=fopen($filePathName,'r');
 while (($dataRow= fgetcsv($fp,0,";",'"') )!==FALSE)
 {
     print_r($dataRow);
 }




  • 我得到: sterreich作为结果在屏幕上这是错误我切割结果的所有其他部分)。

  • 如果我用notedpad ++打开文件,看看编码,我看到ANSI而不是UTF-8。

  • 如果我将notepad ++中的编码更改为UTF8 ...,则ö,ä,...将被特殊字符替换,我必须手动更正。

  • 如果我走另一个路由并创建一个新的UTF-8文件与notedpad ++和放在与excel文件相同的数据,我得到显示Österreich

    If I go another route and create a new UTF-8 file with notedpad++ and put in the same data as in the excel file I get shown "Österreich" on screen when I open it with the php file.

    现在我的问题是,为什么它不能用excel函数,因此我在这里做错了?我可以忽略某些东西吗?

    Now the question I have is, why does it not function with excel, thus am I doing something wrong here? Or am I overlooking something?

    编辑
    由于程序最终将安装在Windows服务器客户,
    a解决方案是需要的,它不需要安装其他工具(PHP库,...确定,但必须安装一个vm-ware或cygwin,...不是)。
    也不会有一个本地安装在服务器上的excel(或办公室),因为
    客户将能够通过文件上传对话框(对话框本身
    )上传.csv文件不是问题的一部分,因为我知道如何处理这些和额外的问题本身,当我创建一个excel文件,并将其转换为.csv在本地安装excel的testmachine)。

    Tnx

    推荐答案

    从PHP DOC


    此功能会考虑区域设置。如果LANG是例如en_US.UTF-8,一字节编码中的文件被此函数读取错误

    strong>您可以尝试

    You can try

    header('Content-Type: text/html; charset=UTF-8');
    $fp = fopen("log.txt", "r");
    echo "<pre>";
    while ( ($dataRow = fgetcsv($fp, 1000, ";")) !== FALSE ) {
        $dataRow = array_map("utf8_encode", $dataRow);
        print_r($dataRow);
    }
    

    输出

    Array
    (
        [0] => ID
        [1] => englishName
        [2] => germanName
    )
    Array
    (
        [0] => 1
        [1] => Austria
        [2] => Österreich
    )
    

    这篇关于Excel csv导出到具有fgetcsv的php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆