1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravelでエクセルを生成してS3に送信する方法

Posted at

環境

Laravel 5.1
"maatwebsite/excel": "~2.1.0"
PHP7.0

maatwebsiteのエクセルライブラリを使ってエクセルを作成してstringに変換してputする
ファイルのオブジェクトそのままは渡せないので注意

createExcelAndSendS3.php
    $fileName = $date . '.xls';

    $fileContent = Excel::create($fileName , function($excel) use ($licencies) {
        $excel->sheet('Excel', function($sheet) use ($licencies) {
            $sheet->fromArray($licencies);
        });
    })->string('xls');

    Storage::disk('s3')->put($fileName, $fileContent);

ちなみにmaatwebsite/excelのバージョンが3.0以上ならこういう書き方ができる(3.1ならオプションを色々使える)

storeExcelToS3.php
public function storeExcel() 
{
    // Store on a different disk (e.g. s3)
    Excel::store(new InvoicesExport(2018), 'invoices.xlsx', 's3');
    
    // Store on a different disk with a defined writer type. 
    Excel::store(new InvoicesExport(2018), 'invoices.xlsx', 's3', Excel::XLSX);
}

外部とのアクセスはFWの中まで読まないと仕様がわからない時があるなぁ
そもそもExcelのライブラリのバージョンあげられればワンライナーで書けたんだけど・・・

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?