Tuesday, 2 April 2019

Exporting CSV with League/CSV doesn't encode umlauts

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?



from Exporting CSV with League/CSV doesn't encode umlauts

No comments:

Post a Comment