您好,欢迎访问一九零五行业门户网

聊聊Laravel Excel 的五个鲜为人知的功能

laravel excel package 最近发布了 3.0 版本,它所具有的新功能,可以帮助简化高级需求,并且可用性极高。大家一起来探讨一下可能不知道的一些隐藏功能,这些功能使 laravel excel 成为 excel 拓展的最佳首选。
1. 从 html 或者是 blade 导入数据假设已经有一个 html 表格
模版代码 -- resources/views/customers/table.blade.php:
<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>
你可以使用它去重复导入这个表格到 excel
步骤1. 生成一个 export 类
php artisan make:export customersfromview --model=customer
步骤2. 使用 fromview 进行操作
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() ]); }}
这里是导入的 excel 文件:
注意:这里只能导出 html 表格,不能具有任何标签,比如 html,body,div 等。
2. 导出到 pdf,html,或是其他格式的文件虽然包的名称是 laravel excel,但是提供了多种导出格式,并且使用起来十分简单,只要在类里再添加一个参数即可:
return excel::download(new customersexport(), 'customers.xlsx', 'html');
比如这么做,就导出到了html,如下图所示:
没有太多的样式,下面是源代码:
不仅如此,它还可以导出到 pdf,甚至你可以从中选择三种库,使用方法是一样的,你只要在最后一个参数指定格式就好了,下面是一些例子。 文档示例:
注意:你必须通过 composer 安装指定的 pdf 包,比如:
composer require dompdf/dompdf
导出的 pdf 如下所示:
3. 按需格式化单元格laravel excel 有一个强有力的「爸爸」 -- phpspreadsheet。因此它就拥有其各种底层功能,包括各种方式的单元格格式化。
此处是一个如何在 laravel export 类中使用它的例子,例如 app/exports/customersexportstyling.php:
步骤 1. 在头部引入适当的类。
use maatwebsite\excel\concerns\withevents;use maatwebsite\excel\events\aftersheet;
步骤 2. 在 implements 部分使用 withevents 接口。
class customersexportstyling implements fromcollection, withevents{ // ...
步骤 3. 用 aftersheet 事件来创建 registerevents() 方法。
/** * @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中找到所有的以上以及更多示例。
4. 隐藏模型属性假设我们已经创建了laravel 5.7默认的users表:
现在我们尝试用简单的fromcollection来导出用户表数据:
class usersexport implements fromcollection{ public function collection() { return user::all(); }}
在导出的excel 里,你只能看到如下字段,但是没有password和remember_token:
这是因为在user模型里定义了隐藏字段的属性:
class user extends authenticatable{ // ... /** * 这个数组用来定义需要隐藏的字段。 * * @var array */ protected $hidden = [ 'password', 'remember_token', ];}
所以,默认情况下这些字段是隐藏的,如果你想在导出数据的时候某些字段不被导出的话,可以直接在模型中定义隐藏属性$hidden。
5. 公式出于某种原因,laravel excel 包的官方文档中并没有提及公式,但是这是excel 重要的功能!
幸运的是,我们可以直接将公式写在导出数据的类中,我们需要设置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, ]; }}
以上就是laravel excel的五个鲜为人知的功能。
原文地址:https://laravel-news.com/five-hidden-features-of-the-laravel-excel-package
译文地址:https://learnku.com/laravel/t/24161
【相关推荐:laravel视频教程】
以上就是聊聊laravel excel 的五个鲜为人知的功能的详细内容。
其它类似信息

推荐信息