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