I am exporting a model in Laravel 5.7, using the League/CSV package:
public function export(Request $request)
{
$people = Person::all();
$location = 'export.csv';
$csv = Writer::createFromPath($location, 'w');
$csv->setOutputBOM(Writer::BOM_UTF8);
$csv->setDelimiter(';');
foreach ($people as $person) {
$csv->insertOne($this->serializePerson($person));
}
return response($location);
}
protected function serializePerson($person)
{
return [
$person->name,
$person->age,
];
}
This creates the export.csv
file ok, but any umlauts are rendered incorrectly (e.g. as ö
). I would have thought setting the BOM would have solved this. Does anyone have a solution?
EDIT: The problem was not the export, it was Mac Excel displaying the umlaut incorrectly. See my answer below.
My question was badly formulated, but I'll leave the answer here in case anyone else has the same problem. The file was in fact being correctly exported: if I opened it in a text editor the umlaut was correctly formed.
To get it working properly in Excel (on a Mac) I had to:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With