Laravel : UTF-8 issue while exporting CSV

Hola picture Hola · Aug 13, 2016 · Viewed 7.9k times · Source

While trying to export data form database I saw some garbage value instead of showing the actual data which is UTF-8 character. I used Excel for opening the csv and I used Maatwebsite/Laravel-Excel package for exporting the csv.

Here is my controller :

public function downloadExcel($type)
    {
        $data = Item::get()->toArray();
        return Excel::create('solutionstuff_example', function($excel) use ($data) {

        header('Content-Encoding: UTF-8');
        header('Content-type: text/csv; charset=UTF-8');
        header('Content-Disposition: attachment; filename=solutionstuff_example.csv');
            $excel->sheet('mySheet', function($sheet) use ($data)
            {
                $sheet->fromArray($data);
            });
        })->download($type);
    }

Here is snap of it:

enter image description here

If anybody face the problem and know how to fix it.Hope you'll help me to solve it. Thanks

Answer

Nguyen Hung picture Nguyen Hung · Dec 28, 2016

I have solved the problem:

I changed the value: 'use_bom'=>false to 'use_bom'=>true

in the 'csv' part of excel.php file in config folder. I wish you success!

Image details:

enter image description here