Rumah > Artikel > pembangunan bahagian belakang > 解决PHP生成UTF-8编码的CSV文件用Excel打开乱码的有关问题
解决PHP生成UTF-8编码的CSV文件用Excel打开乱码的问题
<?php function writeCsvToFile($file,array $data){ $fp = fopen($file, 'w'); //Windows下使用BOM来标记文本文件的编码方式 fwrite($fp,chr(0xEF).chr(0xBB).chr(0xBF)); foreach ($data as $line) { fputcsv($fp, $line); } fclose($fp); } $file = "./testcsv.csv"; $data = array( array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), array(1,'色色',12345,'vb200'), ); writeCsvToFile($file,$data);?