Is it possible to force Excel recognize UTF-8 CSV files automatically?

后端 未结 27 2046
醉梦人生
醉梦人生 2020-11-21 22:27

I\'m developing a part of an application that\'s responsible for exporting some data into CSV files. The application always uses UTF-8 because of its multilingual nature at

27条回答
  •  野的像风
    2020-11-21 22:49

    Working solution for office 365

    • save in UTF-16 (no LE, BE)
    • use separator \t

    Code in PHP

    $header = ['číslo', 'vytvořeno', 'ěščřžýáíé'];
    $fileName = 'excel365.csv';
    $fp = fopen($fileName, 'w');
    fputcsv($fp, $header, "\t");
    fclose($fp);
    
    $handle = fopen($fileName, "r");
    $contents = fread($handle, filesize($fileName));
    $contents = iconv('UTF-8', 'UTF-16', $contents);
    fclose($handle);
    
    $handle = fopen($fileName, "w");
    fwrite($handle, $contents);
    fclose($handle);
    

提交回复
热议问题