首页 > 解决方案 > 使用 Laravel Excel 在 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);
        });
});

然后我在我的导出类中使用它:

...
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?用一种更简单的方式来做造型。

谢谢!

标签: phpspreadsheetlaravel-excel

解决方案


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


推荐阅读