如何在laravel Maatwebsite excel中读取工作表名称

如何在laravel Maatwebsite excel中读取工作表名称,laravel,maatwebsite-excel,Laravel,Maatwebsite Excel,我有一个包含多个工作表的excel工作表,我想获取当前活动的工作表的名称我认为$event->getSheet()->getTitle()应该可以工作。您还可以使用事件 class HImport1 implements ToCollection, WithHeadingRow, WithEvents { public $sheetNames; public $sheetData; public function __construct(){ $this

我有一个包含多个工作表的excel工作表,我想获取当前活动的工作表的名称

我认为
$event->getSheet()->getTitle()
应该可以工作。您还可以使用事件

class HImport1 implements ToCollection, WithHeadingRow, WithEvents
{
    public $sheetNames;
    public $sheetData;

    public function __construct(){
        $this->sheetNames = [];
    $this->sheetData = [];
    }
    public function collection(Collection $collection)
    {
        $this->sheetData[] = $collection;
    }
    public function registerEvents(): array
    {
        return [
            BeforeSheet::class => function(BeforeSheet $event) {
                $this->sheetNames[] = $event->getSheet()->getTitle();
            } 
        ];
    }

}

在3.1版中,API略有更改。在获取标题之前,您必须调用
getDelegate()
。在@Satendra Rawat answer的基础上建造

公共函数registerEvents():数组
{
返回[
BeforeSheet::class=>函数(BeforeSheet$事件){
$this->sheetNames[]=$event->getSheet()->getDelegate()->getTitle();
}
];
}