laravel csv,如何获得前面零的csv文件 [英] laravel csv , how to get csv file with front zeros

查看:191
本文介绍了laravel csv,如何获得前面零的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我生成我的csv文件,一些特殊的字符和前面的零单元格值没有得到。 pls建议如何解决此问题

when I generate my csv file , some of the special charactors and front zero of cell values are not getting . pls advice how to fix this

    public function csv()
    {
        // $table = Festivals::all();
        $headers = [
            'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
            , 'Content-type' => 'text/csv'
            , 'Content-Disposition' => 'attachment; filename=sss.csv'
            , 'Expires' => '0'
            , 'Pragma' => 'public'
        ];

              $list = Festivals::select('ref_no','artist_name','postal_address','payment','payment_fee','bank_details','bank_name','bank_address','account_name','account_number','sort_code','reg_vat','vat_reg_number'
,'foreign_artise','name_desg','email','mobile','name_address','vehicle_size','backline','additional_backline','name','date')->get()->toArray();


        # add headers for each column in the CSV download
        array_unshift($list, array_keys($list[0]));

        $callback = function () use ($list) {
            $FH = fopen('php://output', 'w');
            foreach ($list as $row) {
                fputcsv($FH, $row);
            }
            fclose($FH);
        };

        return Response::stream($callback, 200, $headers);
    }

EDIT

推荐答案

Tharu,

您的数据看起来像这样: fputcsv($ FH,$ row);

Well I'm not sure what your data looks like at this point: fputcsv($FH, $row);

做一些假设,我们可以从那里。

However, I'll make a few assumptions and we can go from there.

首先,你的零可能已经在 $ row array。但现在,请添加上面的 fputcsv($ FH,$ row);

First, it's possible that your zeros are already missing within the $row array. But for now, please add this above fputcsv($FH, $row);:

foreach ($row as $key => $val) {
    $row[$key] = '="'.str_replace('"', '\"', $val).'"';
}

看看你有多少钱。如果仍然无法正常工作,请告诉我当前输出的一行,并让我知道是什么问题。

See how much that does for you. If it's still not working right, please show me a line of the current output, and let me know what is wrong with it.

EDIT

我的意思是:

$callback = function () use ($list) {
    $FH = fopen('php://output', 'w');
    foreach ($list as $row) {
        foreach ($row as $key => $val) {
            $row[$key] = '="'.str_replace('"', '\"', $val).'"';
        }
        fputcsv($FH, $row);
    }
    fclose($FH);
};

这篇关于laravel csv,如何获得前面零的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆