Laravel : UTF-8 issue while exporting CSV

浪子不回头ぞ 提交于 2019-12-08 07:19:21

问题


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:

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


回答1:


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:



来源:https://stackoverflow.com/questions/38929492/laravel-utf-8-issue-while-exporting-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!