duanluan8390 2015-06-06 12:14
浏览 599

使用PHP fputcsv函数导出CSV文件的正确方法

What I want to do is, to export some dataset for Excel without using extra, 3rd party, heavy libraries.

The problem is, when I export the file, first row looks well, but starting from second row, it puts all $row data into first field of current row.

So I get file with first row properly placed in right columns and starting from second row instead of columns I see whole text in first field seperated by delimiter (comma)

enter image description here

Here is code snippet that I use for result.

        $counter=0;
        $file = fopen("sample.csv", "w");
        foreach ($registrants as $registrant) {
            $row = [
                'Fullname' => $registrant->fullname,
                'Phone' => $registrant->phone,
                'Email' => $registrant->email,
            ];
            if ($counter == 0)
                fputcsv($file, array_keys($row));
            fputcsv($file, $row);
            $counter++;
        }
        fclose($file);

Also tried to

         fputcsv($file, array_values($row), ';', ' ');

No success. What am I doing wrong? What is proper way to see correct result on all Excel versions regardless of OS or Excel locale and etc.?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
    • ¥15 onvif+openssl,vs2022编译openssl64
    • ¥15 iOS 自定义输入法-第三方输入法
    • ¥15 很想要一个很好的答案或提示
    • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
    • ¥15 怀疑手机被监控,请问怎么解决和防止
    • ¥15 Qt下使用tcp获取数据的详细操作
    • ¥15 idea右下角设置编码是灰色的
    • ¥15 全志H618ROM新增分区
    • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示