インストール
下記のコマンドを実行します。
1 |
$ composer require maatwebsite/excel |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
[hoge@sv sample]$ composer require maatwebsite/excel Info from https://repo.packagist.org: #StandWithUkraine ./composer.json has been updated Running composer update maatwebsite/excel Loading composer repositories with package information Updating dependencies Lock file operations: 8 installs, 0 updates, 0 removals - Locking composer/semver (3.3.2) - Locking ezyang/htmlpurifier (v4.16.0) - Locking maatwebsite/excel (3.1.48) - Locking maennchen/zipstream-php (v2.4.0) - Locking markbaker/complex (3.0.2) - Locking markbaker/matrix (3.0.1) - Locking myclabs/php-enum (1.8.4) - Locking phpoffice/phpspreadsheet (1.28.0) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 8 installs, 0 updates, 0 removals - Downloading maennchen/zipstream-php (v2.4.0) - Downloading phpoffice/phpspreadsheet (1.28.0) - Downloading maatwebsite/excel (3.1.48) - Installing markbaker/matrix (3.0.1): Extracting archive - Installing markbaker/complex (3.0.2): Extracting archive - Installing myclabs/php-enum (1.8.4): Extracting archive - Installing maennchen/zipstream-php (v2.4.0): Extracting archive - Installing ezyang/htmlpurifier (v4.16.0): Extracting archive - Installing phpoffice/phpspreadsheet (1.28.0): Extracting archive - Installing composer/semver (3.3.2): Extracting archive - Installing maatwebsite/excel (3.1.48): Extracting archive 6 package suggestions were added by new dependencies, use `composer suggest` to see details. Package fabpot/goutte is abandoned, you should avoid using it. Use symfony/browser-kit instead. Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi INFO Discovering packages. inertiajs/inertia-laravel .............................................................................................. DONE kyslik/column-sortable ................................................................................................. DONE laravel/sail ........................................................................................................... DONE laravel/sanctum ........................................................................................................ DONE laravel/tinker ......................................................................................................... DONE maatwebsite/excel ...................................................................................................... DONE nesbot/carbon .......................................................................................................... DONE nunomaduro/collision ................................................................................................... DONE nunomaduro/termwind .................................................................................................... DONE spatie/laravel-ignition ................................................................................................ DONE tightenco/ziggy ........................................................................................................ DONE 89 packages you are using are looking for funding. Use the `composer fund` command to find out more! > @php artisan vendor:publish --tag=laravel-assets --ansi --force INFO No publishable resources for tag [laravel-assets]. Found 1 security vulnerability advisory affecting 1 package. Run composer audit for a full list of advisories. Using version ^3.1 for maatwebsite/excel |
config/app.php
config/app.phpに、service providerとaliaseに下記のように追記します。
1 2 3 4 5 6 7 8 9 10 11 |
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ] 'aliases' => [ ... 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ] |
autoloadの設定
1 |
$ composer dump-autoload |
1 2 3 4 5 6 7 8 9 10 11 |
Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi INFO Discovering packages. ・・・省略・・・ maatwebsite/excel ...................................................................................................... DONE ・・・省略・・・ Generated optimized autoload files containing 6852 classes |
設定ファイルの作成
以下のコマンドを実行し、excel用の設定ファイルを作成します。
1 |
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config |
app/configにexcel.phpが作成されています。
1 2 3 4 5 6 7 |
Copying file [vendor/maatwebsite/excel/src/Console/stubs/export.model.stub] to [stubs/export.model.stub] ............... DONE Copying file [vendor/maatwebsite/excel/src/Console/stubs/export.plain.stub] to [stubs/export.plain.stub] ............... DONE Copying file [vendor/maatwebsite/excel/src/Console/stubs/export.query.stub] to [stubs/export.query.stub] ............... DONE Copying file [vendor/maatwebsite/excel/src/Console/stubs/export.query-model.stub] to [stubs/export.query-model.stub] ... DONE Copying file [vendor/maatwebsite/excel/src/Console/stubs/import.collection.stub] to [stubs/import.collection.stub] ..... DONE Copying file [vendor/maatwebsite/excel/src/Console/stubs/import.model.stub] to [stubs/import.model.stub] ............... DONE Copying file [vendor/maatwebsite/excel/config/excel.php] to [config/excel.php] ......................................... DONE |
インポート
excel.blade.php
resources/viewsの中にexcel.blade.php を作成します。
1 2 3 4 5 6 7 |
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data"> @csrf <div class="form-group"> <input type="file" name="file"> <button class="btn btn-success">Excelインポート</button> </div> </form> |
インポートファイルの作成
以下コマンドでインポートファイルを作成します。
1 |
php artisan make:import ExcelImport --model=ExcelData |
app/Imports にExcelImport.phpファイルが作成されます。
INFO Import [app/Imports/ExcelImport.php] created successfully.
ヘッダーを含める場合
ヘッダーを含める場合には、下記の記述が必要になります。
1 2 3 4 5 6 7 8 9 10 11 |
use Maatwebsite\Excel\Concerns\WithHeadingRow; class ExcelImport implements ToModel,WithHeadingRow public function model(array $row) { return new モデル名([ ‘データベースのカラム名‘=> $row[‘エクセルのカラム名‘], ]); } } |
コントローラーの設定
コントローラー用に以下のコマンドを実行します。
1 |
php artisan make:controller ExcelController |
app\ControllersにExcelControllerが作成されます。
そして、ExcelController.phpの先頭に以下を追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
use App\Imports\ExcelImport; use Maatwebsite\Excel\Facades\Excel; 最後に表示用・インポート用のメソッドを追加します。 //ページ表示用 public function excel(){ return view('excel'); } //ページインポート用 public function excelImport(){ //ファイルが選択されていない場合 if(request()->file('file') == false){ return back()->with('importMessage', 'ファイルを選択してください'); } Excel::import(new ExcelImport, request()->file('file')); return back()->with('importMessage', 'インポートが完了しました。'); } |
公式サイト
Supercharged Excel exports and imports in Laravel | Laravel Excel
Laravel Excel is intended at being Laravel-flavoured PhpSpreadsheet: a simple, but elegant wrapper with the goal of simp...
参考サイト
Laravel9でExcelファイルをインポート/エクスポートする方法【maatwebsite】
https://www.its-corp.co.jp/laravel9-excel-import-export/