excel - 如何在 maatwebsite 3.1 的多个表单中使用 withEvents 应用样式?

标签 excel laravel-6 maatwebsite-excel

我以前使用过 laravel excel 2.1 版。在 2.0 版中,单元设计非常简单。所以我在最新版本的 laravel (6) 中开始了一个新项目,很震惊地知道我们不能在这个版本的 laravel 中使用 laravel excel 2.0 版。我安装了最新版本的 laravel-excel 3.1,并且工作表样式与 larave-excel 2.0 版本完全不同,所以我深入研究文档,我发现这种自定义样式使用 registerEvents 处理多张工作表,但它不起作用。

这是我的导出课。

<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithEvents;
use Excel;
use App\Exports\ExportSheet;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Sheet;


class ExcelExport implements WithMultipleSheets, WithEvents
{
    use Exportable;
    /**
    * @var Invoice $invoice
    */
    protected $excel_input;

    private $writerType = 'xlsx';

    public function __construct($excel_input)
    {
        $this->excel_input = $excel_input;
    }

    public function sheets(): array
    {
        $data   = $this->excel_input;
        $sheets = [];

        foreach($data as $sheet_data) {
            $sheets[] =  new ExportSheet($sheet_data);
        }
        return $sheets;
    }

    public function registerEvents(): array
    {
        $normal_style = array('font' => array('name' => 'Times New Roman','size' => 15));

        return [
            AfterSheet::class => function(AfterSheet $event) use($normal_style) {
                $event->sheet->getStyle("A:Z")->getAlignment()->setWrapText(true);
                $event->sheet->getStyle("A:Z")->applyFromArray($normal_style);
                $event->sheet->setAutoSize(true);
                $event->sheet->setScale('100');
                $event->sheet->setFitToHeight(false);
                $event->sheet->setFitToWidth(true);
                $event->sheet->setPageMargin(0.25);
            },
        ];
    }

}

这是我的 ExportSheet 文件,用于生成多张工作表。
<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Illuminate\Contracts\View\View;
use PhpOffice\PhpSpreadsheet\Style\Style;

class ExportSheet implements WithTitle, ShouldAutoSize, FromView, WithColumnFormatting
{
    private $sheet_data;


    public function __construct(array $sheet_data)
    {
        $this->sheet_data = $sheet_data;
    }

    /**
     * @return string
     */
    public function title(): string
    {
        $data = $this->sheet_data;
        return $data['sheet_name'];
    }

    public function ShouldAutoSize()
    {
        return true;
    }

    public function view(): View
    {
        $info = $this->sheet_data;
        return view($info['template_name'])->with(['data'=>$info]);
    }
}```

最佳答案

我有同样的问题。 registerEvents没有 implements WithMultipleSheets 工作正常.但是在 implements WithMultipleSheets 之后,它不再起作用了。

请问有人给我一些建议吗?

关于excel - 如何在 maatwebsite 3.1 的多个表单中使用 withEvents 应用样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60148318/

相关文章:

VBA 返回#REF!粘贴值时

php - 拉维尔 6 : Cannot send message without a sender address

php - 如何在使用 Maatwebsite excel 导出时删除数组键?

php - 在嵌套 foreach 循环内使用 MySQL 查询的缓慢 Excel 报告 [Laravel 5.3]

vba - 将合并特定工作表复制到一个工作簿

vba - Excel VBA 更改复制的事件工作表的名称

excel - 外部数据表上的 HLOOKUP - 错误的标题行

php - 在 Laravel 中使用 RefreshDatabase 时尝试访问 null 类型值的数组偏移量

php - laravel post 500(内部服务器错误)?

php - 在 laravel 中上传时如何验证 csv 文件?