【问题标题】:How to read the worksheet name in laravel Maatwebsite excel如何在 laravel Maatwebsite excel 中读取工作表名称
【发布时间】:2021-01-14 10:19:54
【问题描述】:

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

【问题讨论】:

    标签: laravel maatwebsite-excel


    【解决方案1】:

    我认为$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();
                } 
            ];
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      在 3.1 版中,API 略有变化。您必须先致电getDelegate(),然后才能获得标题。以@Satendra Rawat 回答为基础...

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

      【讨论】:

      • 是否有跳过工作表并根据在 DB 中检查工作表名称来获取数据
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2013-08-27
      • 2017-11-04
      • 2018-01-30
      • 2021-08-22
      • 2020-09-25
      • 2020-12-30
      相关资源
      最近更新 更多