您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpspreadsheet实现exce中l下拉选择
发布时间:2022-11-14 21:04:31编辑:雪饮阅读()
之前有实现过一个版本,但是呢,那个版本哈是在php7.4不可用,今天这个支持php7.4
环境:php7.4 composer "phpoffice/phpspreadsheet": "^1.23
这里以tp6实现为例哈
public function generate_excel_with_select(){
$spreadsheet = new Spreadsheet();
//获取激活态工作表
$sheet = $spreadsheet->getActiveSheet();
//设置单元格头
$sheet->setCellValue('A1', '分类');
$sheet->setCellValue('B1', '所属父类');
//设置第一行颜色
$sheet->getStyle('A1:E1')
->getFill()
->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setRGB('FFFF00');
$excel_bank_arr = ['父级分类001',"父级分类002"];
//后面行循环放内容
for($i=2;$i<10;$i++){
//父级分类名下拉生成
$list = implode(',', $excel_bank_arr);
$objValidation1 =$sheet->getCell('B'.$i)->getDataValidation(); //从第二行开始有下拉样式
$objValidation1->setType(\PHPExcel_Cell_DataValidation::TYPE_LIST )
->setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION )
->setAllowBlank(false)
->setShowInputMessage(true)
->setShowErrorMessage(true)
->setShowDropDown(true)
->setErrorTitle('输入的值有误')
->setError('您输入的值不在下拉框列表内.')
->setPromptTitle('')
->setPrompt('')
->setFormula1('"' . $list . '"');
}
$writer = new Xlsx($spreadsheet);
$full_path=$_SERVER["DOCUMENT_ROOT"].'hello_world.xlsx';
$writer->save($full_path);
$file_content=file_get_contents($full_path);
unlink($full_path);
return download($file_content, 'hello_world.xlsx', true);
}
关键字词:phpspreadsheet,excel,下拉