在此示例中,我們將使用maatwebsite/excel composer程序包執行導入和導出任務。 maatwebsite/excel提供了使用數據庫模型進行導入和導出的簡便方法。 我們使用了maatwebsite/excel 版本3,它提供了從數據庫導入導出數據的好方法,因此首先要執行幾個步驟以獲取示例。
第1步:安裝Laravel 8
在這里,我們需要使用下面命令安裝Laravel 8應用程序:
composer create-project --prefer-dist laravel/laravel blog
第2步:安裝maatwebsite/excel包
在這一步中,我們需要通過composer軟件包管理器安裝maatwebsite/excel軟件包,因此請執行以下命:
composer require maatwebsite/excel
現在打開config/app.php文件,並添加服務提供商(service provider)和別名(aliase)。
config/app.php
'providers' => [
....
Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [
....
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
步驟3:創建虛擬數據
在這一步中,我們需要帶有一些虛擬數據,因此我們可以簡單地導入和導出。因此,首先您必須使用以下命令運行laravel提供的默認遷移:
php artisan migrate
之后,我們需要運行以下命令以生成虛擬用戶:
php artisan tinker
User::factory()->count(20)->create()
步驟4:添加路由
在這一步中,我們需要創建導入導出文件的路徑。因此,打開您的"routes/web.php"文件並添加以下路由。
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MyController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('importExportView', [MyController::class, 'importExportView']);
Route::get('export', [MyController::class, 'export'])->name('export');
Route::post('import', [MyController::class, 'import'])->name('import');
步驟5:創建導入類
在maatwebsite 3版本中,提供了構建導入類的方法,我們必須在控制器中使用。因此,這將是創建新的Import類的好方法。因此,您必須運行以下命令並在該文件上更改以下代碼:
php artisan make:import UsersImport --model=User
app/Imports/UsersImport.php
<?php
namespace App\Imports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class UsersImport implements ToModel, WithHeadingRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new User([
'name' => $row['name'],
'email' => $row['email'],
'password' => \Hash::make($row['password']),
]);
}
}
步驟6:創建導出類
maatwebsite 3版本提供了構建導出類的方法,我們必須在控制器中使用。因此,這將是創建新的Export類的好方法。因此,您必須運行以下命令並在該文件上更改以下代碼:
php artisan make:export UsersExport --model=User
app/Exports/UsersExport.php
<?php
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return User::all();
}
}
步驟7:創建控制器
在此步驟中,現在我們應該在此路徑"app/Http/Controllers/MyController.php"中將新控制器創建為MyController。該控制器將管理所有importExportView,導出和導入請求以及返回響應,因此將以下內容放入控制器文件中:
app/Http/Controllers/MyController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Exports\UsersExport;
use App\Imports\UsersImport;
use Maatwebsite\Excel\Facades\Excel;
class MyController extends Controller
{
/**
* @return \Illuminate\Support\Collection
*/
public function importExportView()
{
return view('import');
}
/**
* @return \Illuminate\Support\Collection
*/
public function export()
{
return Excel::download(new UsersExport, 'users.xlsx');
}
/**
* @return \Illuminate\Support\Collection
*/
public function import()
{
Excel::import(new UsersImport,request()->file('file'));
return back();
}
}
步驟8:創建視圖文件
在最后一步中,讓我們為布局創建import.blade.php(resources/views/import.blade.php),我們將在此處編寫設計代碼並放入以下代碼:
resources/views/import.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 Import Export Excel to database Example - 無涯教程</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<div class="card bg-light mt-3">
<div class="card-header">
Laravel 8 Import Export Excel to database Example - 無涯教程
</div>
<div class="card-body">
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="file" class="form-control">
<br>
<button class="btn btn-success">Import User Data</button>
<a class="btn btn-warning" href="{{ route('export') }}">Export User Data</a>
</form>
</div>
</div>
</div>
</body>
</html>
現在我們可以運行示例了,然后運行下面命令,然后快速運行:
php artisan serve
現在,您可以在瀏覽器上打開以下URL:
http://localhost:8000/importExportView
https://www.learnfk.com/article-laravel-8-import-export-excel-and-csv-file-tutorialexample