Laravel 8 - Excel和CSV

Laravel 8 - Excel和CSV 首页 / Laravel8入门教程 / Laravel 8 - Excel和CSV

在此示例中,无涯教程将使用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 UsersImportrequest()->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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Go语言核心36讲 -〔郝林〕

玩转Git三剑客 -〔苏玲〕

大厂晋升指南 -〔李运华〕

编程高手必学的内存知识 -〔海纳〕

中间件核心技术与实战 -〔丁威〕

现代React Web开发实战 -〔宋一玮〕

后端工程师的高阶面经 -〔邓明〕

PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕

手把手带你写一个 MiniTomcat -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)