Laravel excel library(Maatwebsite) : How to create a drop down list in exports

前端 未结 2 637
囚心锁ツ
囚心锁ツ 2021-01-17 14:56

I am creating an excel template which should contain a dropdown list. I see its possible with the phpexcel library (PHPExcel Multiple Dropdown list that dependent). I was w

2条回答
  •  天涯浪人
    2021-01-17 15:38

      public function index() {
            \Excel::create('file', function($excel) {
                require_once("/apppath//vendor/phpoffice/phpexcel/Classes/PHPExcel/NamedRange.php");
                require_once("/apppath/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell/DataValidation.php");
    
                $excel->sheet('New sheet', function($sheet) {
    
                    $sheet->SetCellValue("A1", "UK");
                    $sheet->SetCellValue("A2", "USA");
    
                    $sheet->_parent->addNamedRange(
                            new \PHPExcel_NamedRange(
                            'countries', $sheet, 'A1:A2'
                            )
                    );
    
    
                    $sheet->SetCellValue("B1", "London");
                    $sheet->SetCellValue("B2", "Birmingham");
                    $sheet->SetCellValue("B3", "Leeds");
                    $sheet->_parent->addNamedRange(
                            new \PHPExcel_NamedRange(
                            'UK', $sheet, 'B1:B3'
                            )
                    );
    
                    $sheet->SetCellValue("C1", "Atlanta");
                    $sheet->SetCellValue("C2", "New York");
                    $sheet->SetCellValue("C3", "Los Angeles");
                    $sheet->_parent->addNamedRange(
                            new \PHPExcel_NamedRange(
                            'USA', $sheet, 'C1:C3'
                            )
                    );
                    $objValidation = $sheet->getCell('D1')->getDataValidation();
                    $objValidation->setType(\PHPExcel_Cell_DataValidation::TYPE_LIST);
                    $objValidation->setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION);
                    $objValidation->setAllowBlank(false);
                    $objValidation->setShowInputMessage(true);
                    $objValidation->setShowErrorMessage(true);
                    $objValidation->setShowDropDown(true);
                    $objValidation->setErrorTitle('Input error');
                    $objValidation->setError('Value is not in list.');
                    $objValidation->setPromptTitle('Pick from list');
                    $objValidation->setPrompt('Please pick a value from the drop-down list.');
                    $objValidation->setFormula1('countries'); //note this!
                });
            })->download("xlsx");
            return view('home');
        }
    

提交回复
热议问题