0

I have PHP string that is saved to a CSV file. Content of the string is splited by \t character. Tab separation is correctly saved, but if I open file in Excel, all of the content is placed (showed) in same cell.

Can you help me how to have file correctly showed by Excel also.

Will it be with some different headers of something like that?

$result = '';

$result .= "\n";
$result .= 'COL1';
$result .= "\t";
$result .= 'COL2';
$result .= "\t";
$result .= 'COL3';
$result .= "\n";

echo $result;

header("Content-type: application/vnd.ms-excel; charset=utf-8");
header('Content-Disposition: attachment; filename="opendata.csv"');

Thank you in advance!

user198003
  • 11,029
  • 28
  • 94
  • 152
  • Possible duplicate of [Create a CSV File for a user in PHP](https://stackoverflow.com/questions/217424/create-a-csv-file-for-a-user-in-php) – Pankaj Makwana Jun 27 '18 at 06:47
  • Main problem is that `CSV` is **not** Excel's default format. Excel has an import wizard which is able opening each kind of CSV properly. But without that wizard using `File - Open` or doubleclick for opening CSV in Excel, the CSV must fulfilling some requirements. For tab separated files it must be UTF-16 encoded for example. – Axel Richter Jun 27 '18 at 06:53

0 Answers0