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

梦想的初衷 提交于 2019-12-23 07:35:20

问题


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 wondering if it could be done with the laravel-excel library provided by maatwebsite. I need the syntax for functions like dropdown,NamedRange, datavalidation,setFormula, etc.


回答1:


  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');
    }


来源:https://stackoverflow.com/questions/29815227/laravel-excel-librarymaatwebsite-how-to-create-a-drop-down-list-in-exports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!