Laravel Excel package 最近發布了 3.0 版本,它所具有的新功能,可以幫助簡化進階需求,可用性極高。大家一起來探討一些可能不知道的隱藏功能,這些功能使 Laravel Excel 成為 Excel 拓展的最佳首選。
<table class="table"> <thead> <tr> <th></th> <th>First name</th> <th>Last name</th> <th>Email</th> <th>Created at</th> <th>Updated at</th> </tr> </thead> <tbody> @foreach ($customers as $customer) <tr> <td>{{ $customer->id }}</td> <td>{{ $customer->first_name }}</td> <td>{{ $customer->last_name }}</td> <td>{{ $customer->email }}</td> <td>{{ $customer->created_at }}</td> <td>{{ $customer->updated_at }}</td> </tr> @endforeach </tbody> </table>
php artisan make:export CustomersFromView --model=Customer
namespace App\Exports; use App\Customer; use Illuminate\Contracts\View\View; use Maatwebsite\Excel\Concerns\FromView; class CustomersExportView implements FromView { public function view(): View { return view('customers.table', [ 'customers' => Customer::orderBy('id', 'desc')->take(100)->get() ]); } }
return Excel::download(new CustomersExport(), 'customers.xlsx', 'Html');
文件範例:
注意:你必須透過composer 安裝指定的PDF 包,例如:composer require dompdf/dompdf
PhpSpreadsheet 。因此它就擁有其各種底層功能,包括各種方式的單元格格式化。
這裡有一個如何在 Laravel Export 類別中使用它的例子,例如 app/Exports/CustomersExportStyling.php:步驟 1. 在頭部引入適當的類別。use Maatwebsite\Excel\Concerns\WithEvents; use Maatwebsite\Excel\Events\AfterSheet;
class CustomersExportStyling implements FromCollection, WithEvents { // ...
/** * @return array */ public function registerEvents(): array { return [ AfterSheet::class => function(AfterSheet $event) { // ... 此处你可以任意格式化 }, ]; }
/** * @return array */ public function registerEvents(): array { return [ AfterSheet::class => function(AfterSheet $event) { // 所有表头-设置字体为14 $cellRange = 'A1:W1'; $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14); // 将样式数组应用于B2:G8范围单元格 $styleArray = [ 'borders' => [ 'outline' => [ 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK, 'color' => ['argb' => 'FFFF0000'], ] ] ]; $event->sheet->getDelegate()->getStyle('B2:G8')->applyFromArray($styleArray); // 将第一行行高设置为20 $event->sheet->getDelegate()->getRowDimension(1)->setRowHeight(20); // 设置 A1:D4 范围内文本自动换行 $event->sheet->getDelegate()->getStyle('A1:D4') ->getAlignment()->setWrapText(true); }, ]; }
Recipes page of PhpSpreadsheet docs中找到所有的以上以及更多範例。
Laravel 5.7預設的
users表:
FromCollection來導出用戶表資料:
class UsersExport implements FromCollection { public function collection() { return User::all(); } }
password和
remember_token:
User模型裡定義了隱藏欄位的屬性:
class User extends Authenticatable { // ... /** * 这个数组用来定义需要隐藏的字段。 * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
$hidden。
cell 的值,就像這樣:
=A2 1 or SUM(A1 :A10)。
WithMapping 介面:
use App\Customer; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\WithMapping; class CustomersExportFormulas implements FromCollection, WithMapping { public function collection() { return Customer::all(); } /** * @var Customer $customer * @return array */ public function map($customer): array { return [ $customer->id, '=A2+1', $customer->first_name, $customer->last_name, $customer->email, ]; } }
原文網址:https://laravel-news.com/five-hidden-features-of-the-laravel-excel-package翻譯網址:https:// learnku.com/laravel/t/24161
【相關推薦:laravel影片教學】
以上是聊聊Laravel Excel 的五個鮮為人知的功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!