Export CSV for Excel

前端 未结 5 1235
耶瑟儿~
耶瑟儿~ 2020-12-29 16:58

I\'m writing a CSV file in PHP using fputcsv($file, $data). It all works, however I can\'t just open it in Excel but have to import it and specify the encoding

5条回答
  •  失恋的感觉
    2020-12-29 17:48

    Despite the "C=comma" in CVS, Excel uses your locale native separator. So supposing fputcsv always uses a comma, it won't work, if your locale separator is for example a semicolon.

    What Google AdSense does, when you click "Export to Excel CSV", is that it uses Tab as a separator. And that works.

    To replicate that, set the third parameter (delimiter) of fputcsv to override the default comma. E.g. for Tab use: fputcsv($handle, $fields, "\t");

    Compare the format of the CSV that works for you against the one generated by fputcsv.

    Consider including example of both in your question. You might get better answers.

提交回复
热议问题