Laravel:导出 CSV 时出现 UTF-8 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38929492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:18:38  来源:igfitidea点击:

Laravel : UTF-8 issue while exporting CSV

phplaravelcsvutf-8maatwebsite-excel

提问by Hola

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-Excelpackage for exporting the csv.

在尝试从数据库导出数据时,我看到了一些垃圾值,而不是显示 UTF-8 字符的实际数据。我使用 Excel 打开 csv,并使用Maatwebsite/Laravel-Excel包导出 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

如果有人遇到这个问题并知道如何解决它。希望你能帮助我解决它。谢谢

回答by Nguyen Hung

I have solved the problem:

我已经解决了这个问题:

I changed the value: 'use_bom'=>falseto 'use_bom'=>true

我改变了价值: 'use_bom'=>false'use_bom'=>true

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

在config 文件夹中'csv'excel.php文件部分。祝你成功!

Image details:

图片详情:

enter image description here

在此处输入图片说明