composer require rap2hpoutre/fast-excel
コントローラーから次のように呼び出します
use Rap2hpoutre\FastExcel\FastExcel;
return (new FastExcel(User::all()))->download('file.xlsx');
コントローラーから次のように呼び出します。 (拡張子をCSVにするとCSVでダウンロードされます)
use Rap2hpoutre\FastExcel\FastExcel;
return (new FastExcel(User::all()))->download('file.csv');
↑ このコードでは
・\App\User モデルの全データが出力
・\App\User モデルの全カラムが出力
・文字コード 「UTF-8 With BOM」
・CSVのダブルクォーテーション ““ での 囲みは自動(値にクォーテーションを含むなど必要な時だけクォーテーションがつく。)
でダウンロードされます。
return (new FastExcel(User::all()))->download('file.csv', function ($user) {
return [
'メールアドレス' => $user->email,
'姓' => strtoupper($user->lastname),
'名' => $user->firstname,
];
});
このようにすると3つのカラムのみ出力します。
use Rap2hpoutre\FastExcel\FastExcel;
return (new FastExcel(User::all()))->configureCsv(',', '"', 'UTF-8', false)->download('file.csv');
パラメーターは次の通りです
configureCsv($delimiter = ',', $enclosure = '"', $encoding = 'UTF-8', $bom = false)