thephpleague / csv

CSV data manipulation made easy in PHP
https://csv.thephpleague.com
MIT License
3.34k stars 336 forks source link

No new line in my generated CSVs #476

Closed axtg closed 2 years ago

axtg commented 2 years ago
Q A
OS Ubuntu 22
PHP 80109
Laravel 9.34
Package 9.8

Question

Simplified I'm doing the following:

$csv = Writer::createFromFileObject(new \SplTempFileObject);

$header = ['item1', 'item2'];
$csv->insertOne($header);

foreach($responses as $response) {
    // In a loop, because I'm doing some mutations here per row
    $csv->insertOne([$response->item_x, $response->item_y]);
}

return response()->streamDownload(function () use ($csv) {
     echo $csv;
}, 'results_meeting_'.$meeting->token.'.csv');

This initializes the download, but both header and rows appear in one line. In Excel, VSCode and Notepad I get one row. I've tried setting newline to both \r\n and \n. Which I see in the file, but thus isn't parsed.

Anything I'm missing?

nyamsprod commented 2 years ago

@axtg thanks for using the package.

Did you try to run your code without any other tool involved (ie: without Laravel response) to see if the behaviour was still present or not.

If new lines are present then I would suggest asking for help on a better helping channel dedicated to Laravel application.

axtg commented 2 years ago

@nyamsprod thanks for getting back to me. That was the right nudge in the right direction. I've updated my code above with Laravel's streamDownload() method, which then works. One hick-up I then shortly encountered, for anyone reading, I defined setNewline as '\r\n', but that only works if you use double quotes "\r\n". Fyi.