第一步:在laravel8项目的根目录下:
代码语言:javascript复制composer require maatwebsite/excel
代码语言:javascript复制php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider"
第二步:配置好数据库信息(然后进行数据迁移 数据填充),在项目的根目录下:
代码语言:javascript复制php artisan migrate
先打开DatabaseSeeder.php的注释:
代码语言:javascript复制php artisan db:seed --class=DatabaseSeeder
第三步:
代码语言:javascript复制 php artisan make:export UsersExport --model=User
第四步:
routes/web.php下:
代码语言:javascript复制<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersIndexController;
/*
|--------------------------------------------------------------------------
| 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('/export', [AppHttpControllersIndexController::class,'export']);
//index控制器export方法以export来替代
IndexController.php
代码语言:javascript复制<?php
namespace AppHttpControllers;
use AppExportsUsersExport;
use MaatwebsiteExcelFacadesExcel;
class IndexController extends Controller
{
public function Export()//导出excel文件
{
return Excel::download(new UsersExport(),'users.xlsx');//下载数据库中的表数据users
}
}
第六步:在项目的根目录下:
代码语言:javascript复制php artisan serve