我正在尝试创建Excel CSV Sheet然后下载它。
CSV文件创建完美,但遗憾的是下载无效。
public function download_csv(Request $request) {
if ($request->isMethod('post')) {
if (Auth::check()) {
$user_id = Auth::id();
$customer_id = Auth::user()->customer_id;
$suppliers = Supplier::where('is_active', 1)->get();
$dataCSV = array();
$main = array();
$cnt = 1;
foreach($suppliers as $m) {
$main['S. No'] = $cnt++;
$main['Name'] = $m->Name;
array_push($dataCSV, $main);
$cnt++;
}
$csv_name = $user_id.'_'.rand(10000, 99999);
$csv = Excel::create($csv_name, function($excel) use($dataCSV) {
$excel->sheet('Sheetname', function($sheet) use($dataCSV) {
$sheet->fromArray($dataCSV);
});
});
$csv->store('csv', storage_path('excel/exports/'.$customer_id.'/'));
$file = storage_path('excel/exports/'.$customer_id.'/'.$csv_name.'.csv');
$headers = array(
'Content-Type: application/csv',
);
return Response::download($file, $user_id.'.csv', $headers);
}
}
}
我不明白问题出在哪里。
任何帮助将不胜感激。
由于
答案 0 :(得分:0)
您是否使用过响应类?
use Illuminate\Support\Facades\Response;
选项2:尝试通过其他方法下载它:
return response()->download($pathToFile, $name, $headers);