导入,导出TSV Lavavel

时间:2018-08-23 08:35:20

标签: csv laravel-5

我正在用Laravel导入,导出TSV,但没有解决方案。请帮助我,谢谢。 我正在尝试$headers = [ 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0' , 'Content-type' => 'text/tab-separated-values' , 'Content-Disposition' => 'attachment; filename=category.tsv' , 'Expires' => '0' , 'Pragma' => 'public' ]; array_unshift($result, array_keys($result[0]));

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

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

1 个答案:

答案 0 :(得分:0)

            $headers = [
                'Content-type' => 'text/tsv',
                'Content-Disposition' => 'attachment; filename=file' . date('Y-m-d:H-i-s') . '.tsv',
                'Pragma' => 'no-cache',
                'Expires' => '0'
            ];
            $callback = function () use ($report) {
                $fileHandle = fopen('php://output', 'w');
                foreach ($report as $row) {
                    fputcsv($fileHandle, $row, "\t");
                }
                fclose($fileHandle);
            };
            return response()->stream($callback, 200, $headers);