1

Hello i have utf_8 encoded data but when in export i open it on microsoft excel i get this

Blockquote

here when i opened with notepad enter image description here

and when i open the file as excel i get this encoding from original file 1252 windows

here is my code

    $outputCSv = "";
        
        $someData = array(
    array('one' ,'two' ,'three'),
    array('واحد', 'اثنان', 'ثلاثة'),
    array ('اربعة', 'خمسة', 'ستة'),
    array('سبعة', 'ثمانية', 'تسعة'));

foreach ($someData as $data) {
    $outputCSv .= implode(',', $data) . "\r\n";
}
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=csvTest.csv");
header("Content-Encoding: UTF-8");
echo "\xEF\xBB\xBF"; // UTF-8 BOM  

echo $outputCSv; exit(0); 
Mehdi Ammar
  • 61
  • 1
  • 9
  • This has nothing to do with phpexcel or phpspreadsheet and I don't quite get why you use `application/vnd.ms-excel` mime type - csv ought to be `text/csv` – JoSSte Sep 01 '20 at 13:52
  • i removed the phpexcel and phpspreadsheet tags even if change it to text/csv still the same getting the file in 1252 windows encoding – Mehdi Ammar Sep 01 '20 at 13:57
  • have you tried notepad++ or vscode or some other more advanced editor? – JoSSte Sep 01 '20 at 13:58
  • yeah when i use notepad++ or other editors i get the result i want but only when i opened in microsoft excel i get the wrongs caracters – Mehdi Ammar Sep 01 '20 at 14:00
  • https://www.itg.ias.edu/content/how-import-csv-file-uses-utf-8-character-encoding-0 – JoSSte Sep 01 '20 at 14:01
  • Since CSV is a simple text file then I don't see how you can make it work without having to convert the data from Excel – Alon Eitan Sep 01 '20 at 14:01
  • You can use [PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) and create an `.xls` file that will solve the issue (Unless you must use the CSV format) – Alon Eitan Sep 01 '20 at 14:03
  • yeah cause when in download the file i don't get utf_8 encoding i get windows 1252 encoding even when i add the header – Mehdi Ammar Sep 01 '20 at 14:03
  • I think Excel automagically parses csv as 1252. you need to import it as text isung the wizard if you want it as utf8 - or try tsv instead – JoSSte Sep 01 '20 at 14:07
  • Possible duplicate of https://stackoverflow.com/questions/6002256/is-it-possible-to-force-excel-recognize-utf-8-csv-files-automatically – JoSSte Sep 01 '20 at 14:09
  • Does this answer your question? [Is it possible to force Excel recognize UTF-8 CSV files automatically?](https://stackoverflow.com/questions/6002256/is-it-possible-to-force-excel-recognize-utf-8-csv-files-automatically) – JoSSte Sep 01 '20 at 14:09
  • It's hard to tell for sure but I have the impression that you have white spaces before the output. – Álvaro González Sep 01 '20 at 14:33

1 Answers1

0

While i wasted my time on challenging the encoding i used the output buffering and it worked like charm

ob_clean();
    ob_start();
      $outputCSv = "";
    
    $someData = array(
array('one' ,'two' ,'three'),
array('واحد', 'اثنان', 'ثلاثة'),
array ('اربعة', 'خمسة', 'ستة'),
array('سبعة', 'ثمانية', 'تسعة'));

foreach ($someData as $data) {
    $outputCSv .= implode(',', $data) . "\r\n";
}
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=csvTest.csv");
header("Content-Encoding: UTF-8");
echo "\xEF\xBB\xBF"; // UTF-8 BOM  

echo $outputCSv; 
 ob_end_flush();
exit(0); 
Mehdi Ammar
  • 61
  • 1
  • 9