使用Laravel Excel在Laravel中造型出色

时间:2019-07-10 15:23:26

标签: phpspreadsheet laravel-excel

学习Laravel,我现在要面对Excel导出。

在Intenet Laravel Excel(docs.laravel-excel.com)中搜索似乎是更好的选择,但我发现很难对工作表(颜色,字体,大小等)进行样式设置

我在AppServiceProvider中使用全局事件侦听器:

Sheet::listen(AfterSheet::class, function () {
        Sheet::macro('color_tab', function (Sheet $sheet, string $color) {
            $sheet->getDelegate()->getTabColor()->setRGB($color);
        });
});

然后在Export类中使用它:

...
public function __construct($color) {
    $this->color = $color;
}
...
use RegistersEventListeners;
...
public static function afterSheet(AfterSheet $event) {
    // this is an error because it's a static method!
    $event->sheet->color_tab($this->color);
}

问题是我必须用构造函数中指定的颜色给制表符上色,但是我不能,因为所有这些用于Excel样式设置的方法都是静态的。

我该怎么做?

是否还有另一个好的库可以导出Excel?用更简单的方式进行样式设计。

谢谢!

1 个答案:

答案 0 :(得分:0)

最后,我决定不使用包装Laravel Excel直接使用PhpSpreadSheet库