Laravel Xuất dữ liệu trong Excel và CSV
Để xuất dữ liệu dưới dạng excel và csv trong Laravel, hãy sử dụng maatwebsite/excel 3. 1 gói hàng. Gói Laravel Excel được dự định là PhpSpreadsheet có hương vị của Laravel. một trình bao bọc đơn giản nhưng thanh lịch xung quanh PhpSpreadsheet để đơn giản hóa việc xuất và nhập. PhpSpreadsheet là thư viện được viết bằng PHP thuần túy và cung cấp tập hợp các lớp cho phép chúng ta đọc và ghi vào các loại định dạng tệp bảng tính khác nhau, như Excel và LibreOffice Calc.
Nếu bạn muốn bắt đầu và chạy với chức năng cơ bản của laravel, hãy xem bài viết khác của tôi trên blog web này có tên là Laravel 7 Crud Example From Scratch.
Nếu bạn muốn Tạo PDF Trong Laravel, hãy xem Laravel Tạo PDF Từ Chế độ Xem Ví dụ. Trong ví dụ này, chúng tôi sử dụng gói có tên maatwebsite/excel phiên bản 3. 1. Vì vậy, Laravel 9 và maatwebsite/excel 3 của chúng tôi. 1
Khi sử dụng gói trong ứng dụng của bạn, bạn nên hiểu nó hoạt động như thế nào đằng sau hậu trường. Theo dõi hậu trường sẽ giúp bạn cảm thấy thoải mái và tự tin hơn khi sử dụng tiềm năng tối đa của công cụ
Các tính năng của Laravel Excel
- Chúng tôi có thể dễ dàng xuất các bộ sưu tập sang Excel
- Chúng tôi có thể xuất các truy vấn với phân đoạn tự động để có hiệu suất tốt hơn
- Chúng tôi có thể xếp hàng xuất để có hiệu suất tốt hơn
- Chúng tôi có thể dễ dàng xuất các chế độ xem Blade sang Excel
- Chúng tôi có thể dễ dàng nhập nó vào bộ sưu tập
- Chúng ta có thể đọc tệp Excel theo khối
- Chúng tôi có thể xử lý các phần chèn nhập theo đợt
Yêu cầu
- PHP.
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]
5 - ấu trùng.
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]
6 - Bảng tính Php.
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]
7 - Tiện ích mở rộng PHP
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]
8 đã bật - Tiện ích mở rộng PHP
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]
9 đã bật - Tiện ích mở rộng PHP _______5_______0 đã bật
Bước 1. Cài đặt
Yêu cầu gói sau trong nhà soạn nhạc. json của dự án Laravel 9 của bạn. Lệnh sau sẽ tải xuống gói và PhpSpreadsheet
composer require maatwebsite/excel
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗
Bước 2. Cấu hình gói
_'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
1 được tự động phát hiện và được đăng ký theo mặc định
Nếu bạn muốn tự đăng ký, hãy thêm ServiceProvider vào _______5_______2
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]
Mặt tiền của Excel được tự động phát hiện
Nếu bạn muốn thêm thủ công, hãy thêm Mặt tiền trong 'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
2
'aliases' => [ ... 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ]
Nếu bạn muốn xuất bản cấu hình, hãy chạy lệnh xuất bản của nhà cung cấp
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
➜ laravel6 git:[master] ✗ php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" Copied File [/vendor/maatwebsite/excel/config/excel.php] To [/config/excel.php] Publishing complete. ➜ laravel6 git:[master] ✗
Thao tác này sẽ tạo tệp cấu hình mới có tên 'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
4
Bước 3. Tạo các tệp mô hình và di chuyển
Gõ lệnh sau
php artisan make:model Disneyplus -m
Bây giờ, hãy chuyển đến [dấu thời gian]. tạo_disneypluses_table. php và thêm các cột
public function up[] { Schema::create['disneypluses', function [Blueprint $table] { $table->bigIncrements['id']; $table->string['show_name']; $table->string['series']; $table->string['lead_actor']; $table->timestamps[]; }]; }
Bây giờ, di chuyển cơ sở dữ liệu bằng lệnh sau
php artisan migrate
Bước 4. Tạo một bộ điều khiển và các tuyến đường
Bước tiếp theo là tạo DisneyplusController. tệp php
php artisan make:controller DisneyplusController
Bây giờ, hãy thêm hai tuyến đường bên trong tuyến đường >> web. tệp php
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗0
Bây giờ, hãy tạo hai phương thức bên trong DisneyplusController. tệp php
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗1
Bươc. 5 Tạo file blade form để nhập dữ liệu
Bây giờ, trong thư mục views, hãy tạo biểu mẫu. lưỡi. tệp php. Thêm đoạn mã sau
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗2
Bước 6. Lưu trữ dữ liệu trong cơ sở dữ liệu
Bây giờ, chúng ta sẽ viết hai hàm bên trong DisneyplusController. tệp php
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗3
Vì vậy, trong tệp trên, đầu tiên, chúng tôi đã hiển thị tệp biểu mẫu, sau đó bên trong chức năng lưu trữ, chúng tôi kiểm tra xác thực và sau đó lưu trữ dữ liệu vào cơ sở dữ liệu
Ngoài ra, hãy thêm các trường có thể điền vào bên trong Disneyplus. php tệp mô hình.
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗4
Bây giờ, hãy đi đến tuyến đường này. http. // ấu trùng 6. thử nghiệm/disneyplus hoặc http. //máy chủ cục bộ. 8000/disneyplus.
Bạn sẽ thấy một hình thức. Cố gắng lưu dữ liệu và nếu mọi thứ trong mã đều đúng, bạn sẽ thấy một mục nhập trong cơ sở dữ liệu
Bươc. 7 Tạo tệp xem để hiển thị dữ liệu
Trước khi tạo tệp xem, chúng ta cần thêm một tuyến đường bên trong web. php
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗5
Bây giờ, hãy tạo một tệp dạng xem có tên danh sách. lưỡi. tệp php. Thêm đoạn mã sau
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗6
Bây giờ, hãy thêm mã bên trong hàm index[] của DisneyplusController. tệp php
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗7
Bây giờ, hãy truy cập http. // ấu trùng 6. kiểm tra/disneyplus/list hoặc http. //máy chủ cục bộ. 8000/disneyplus/danh sách
Bạn sẽ thấy danh sách các chương trình
Bước 8. Tạo lớp Xuất khẩu
Bạn có thể thực hiện việc này bằng cách sử dụng lệnh 'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
5
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗8
Có thể tìm thấy tệp trong thư mục 'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
6
Tệp DisneyplusExport. php là như sau
➜ laravel6 git:[master] ✗ composer require maatwebsite/excel Using version ^3.1 for maatwebsite/excel ./composer.json has been updated Loading composer repositories with package information Updating dependencies [including require-dev] Package operations: 4 installs, 0 updates, 0 removals - Installing markbaker/matrix [1.1.4]: Downloading [100%] - Installing markbaker/complex [1.4.7]: Downloading [100%] - Installing phpoffice/phpspreadsheet [1.9.0]: Downloading [100%] - Installing maatwebsite/excel [3.1.17]: Downloading [100%] phpoffice/phpspreadsheet suggests installing mpdf/mpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing tecnickcom/tcpdf [Option for rendering PDF with PDF Writer] phpoffice/phpspreadsheet suggests installing jpgraph/jpgraph [Option for rendering charts, or including charts with PDF or HTML Writers] Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-dompdf Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: maatwebsite/excel Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. ➜ laravel6 git:[master] ✗9
Nếu bạn muốn tạo bản xuất theo cách thủ công, bạn có thể tạo phần sau trong 'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
6
Bước 9. Viết hàm xuất
Bên trong Bộ điều khiển Disneyplus. php tập tin, thêm đoạn mã sau
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]0
Vì vậy, tập tin cuối cùng của chúng tôi trông giống như dưới đây
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]1
Cuối cùng, thêm tuyến đường để có thể truy cập xuất
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]2
Ngoài ra, hãy thêm liên kết vào danh sách Xuất bên trong danh sách. lưỡi. tệp php
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]3
Được rồi, bây giờ cuối cùng hãy truy cập http. // ấu trùng 6. test/disneyplus/list, và bây giờ bạn có thể thấy một liên kết có tên là Xuất