First name | Last name | Created at | Updated at | ||
---|---|---|---|---|---|
{{ $customer->id }} | {{ $customer->first_name }} | {{ $customer->last_name }} | {{ $customer->email }} | {{ $customer->created_at }} | {{ $customer->updated_at }} |
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');
Tidak terlalu banyak gaya, berikut ialah kod sumber: Bukan itu sahaja, ia juga boleh mengeksport ke PDF, malah anda boleh memilih tiga perpustakaan daripada itu, dan kaedah penggunaan adalah sama, anda hanya perlu menentukan format dalam parameter terakhir. Berikut adalah beberapa contoh. Contoh dokumen: Nota: Anda mesti memasang pakej PDF yang ditentukan melalui komposer, contohnya:
composer require dompdf/dompdf
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); }, ]; }
lalai Laravel 5.7
: users
mudah untuk mengeksport data jadual pengguna: FromCollection
class UsersExport implements FromCollection { public function collection() { return User::all(); } }
dan password
: remember_token
: User
class User extends Authenticatable { // ... /** * 这个数组用来定义需要隐藏的字段。 * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
dalam model. $hidden
, seperti ini: 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, ]; } }
Alamat asal: https://laravel-news.com/five-hidden-features-of-the-laravel-excel-package
Alamat terjemahan: https://learnku.com /laravel/t/24161
Atas ialah kandungan terperinci Adakah anda tahu lima fungsi Laravel Excel ini?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!