无法在 php laravel 下载 csv 文件

Not able to download csv file in php laravel

这是我的控制器操作,用于下载 csv 格式的文件备份。但是我无法下载该文件。我无法理解我做错了什么。

public function backup()
{

$dummy_data = array([
        'Name' => 'ABC',
        'Age'  => '26',
        'Sex'  => 'Male',
    ],
    [
        'Name' => 'C S',
        'Age'  => '33',
        'Sex'  => 'Male',
    ],
    [
        'Name' => 'Rose',
        'Age'  => '26',
        'Sex'  => 'Male',
    ],
    [
        'Name' => 'Der',
        'Age'  => '24',
        'Sex'  => 'Male',
    ]
);

$csv =  CSV::fromArray($dummy_data)->render();

return $csv

}

我正在使用这个插件来创建 csv 文件。

https://github.com/mnshankar/csv

更新:

echo $csv;  // Gives following output


HTTP/1.0 200 OK
Cache-Control:       private
Content-Disposition: attachment; filename="export.csv"
Content-Type:        text/csv
Date:                Wed, 14 Jan 2015 12:29:04 GMT
Pragma:              cache

Name,Age,Sex
ABC,26,Male
"C S",33,Male
Rose,26,Male
Der,24,Male

试试这个...

我试过它对我有用。

https://github.com/Maatwebsite/Laravel-Excel

导入:

Excel::load('assets/uploads/mti.xlsx', function($reader) {

    // Getting all results
    $results = $reader->get()->toArray();

    // ->all() is a wrapper for ->get() and will work the same
   // $results = $reader->all();
    print_r(array_filter($results));

});

导出:

Excel::create('Filename', function($excel) {

    $excel->sheet('Sheetname', function($sheet) {

        $sheet->fromArray(array(
            array('data1', 'data2'),
            array('data3', 'data4')
        ));

    });

})->export('xls');  

参考:

http://www.maatwebsite.nl/laravel-excel/docs/import

http://www.maatwebsite.nl/laravel-excel/docs/export