laravel-excel匯出excel下拉選擇框

愛餘生發表於2021-06-17

匯出類實現WithEvents

implements WithEvents

事件註冊

/**
     * 通過事件機制,對 excel 的欄位進行一些限制
     * @inheritDoc
     */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $sheet = $event->sheet;
                $col = 'F';
                $list = ['投保', '自定義']
                $count = count($this->data) + 2;
                for ($i = 2; $i <= $count; $i++) {
                    $key = $col . $i;
                    $validation = $sheet->getDelegate()->getCell($key)->getDataValidation();
                    $validation->setType(DataValidation::TYPE_LIST);
                    $validation->setErrorStyle(DataValidation::STYLE_INFORMATION);
                    $validation->setAllowBlank(false);
                    $validation->setShowInputMessage(true);
                    $validation->setShowErrorMessage(true);
                    $validation->setShowDropDown(true);
                    $validation->setErrorTitle('');
                    $validation->setError('');
                    $validation->setPromptTitle('');
                    $validation->setPrompt('');
                    $validation->setFormula1('"' . implode(',', $list) . '"');
                }
            },
        ];
    }
本作品採用《CC 協議》,轉載必須註明作者和本文連結
愛餘生

相關文章