![](https://img-blog.csdn.net/20170213102406336?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1eHVleWk1MjE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
说明:
1、需要准备好phpexcel,下载对应的包可百度;
2、这里重点说明 setFormula 的应用;
3、直接设置下拉选择的值时,如果包含右括号')'则会有问题,可通过引用单元格方式规避;
4、如果你不引用单元格方式能包含右括号的话,欢迎赐教;
//phpexcel 在zentao中案例
$objExcel = $this->app->loadClass('PHPExcel');
// 创建文件格式写入对象实例, uncomment
$objWriter = new PHPExcel_Writer_Excel5($objExcel); // 用于其他版本格式
$objExcel->setActiveSheetIndex(0);
$objActSheet = $objExcel->getActiveSheet();
//设置A1单元格的选择列表
$objValidation = $objActSheet->getCell("A1")->getDataValidation();
$objValidation -> setType(PHPExcel_Cell_DataValidation::TYPE_LIST)
-> setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
-> setAllowBlank(false)
-> setShowInputMessage(true)
-> setShowErrorMessage(true)
-> setShowDropDown(true)
-> setErrorTitle('输入的值有误')
-> setError('您输入的值不在下拉框列表内.')
-> setPromptTitle('设备类型')
//把sheet名为mySheet2的A1,A2,A3作为选项
-> setFormula1('mySheet2!$A$1:$A$3');
//设置为具体的内容
// -> setFormula1('"select1,select2,select3"');
//设置为变量内容,例如:$myStr = 'select1,select2,select3'
// -> setFormula1('"'.$myStr.'"');
//设置单元格颜色
$objStyleA1 = $objActSheet ->getStyle('A1');
$objStyleA1 ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
//设置CELL填充颜色
$objFillA1 = $objStyleA1->getFill();
$objFillA1->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objFillA1->getStartColor()->setARGB('FFcdcdff');
//设置当前活动sheet的名称
$objActSheet->setTitle('mySheet1');
//新增一个sheet,命名为mySheet2
$objExcel->createSheet();
// Add some data to the second sheet, resembling some different data types
$objExcel->setActiveSheetIndex(1);
$objExcel->getActiveSheet()->setCellValue('A1', 'item1');
$objExcel->getActiveSheet()->setCellValue('A2', 'item2');
$objExcel->getActiveSheet()->setCellValue('A3', 'item3');
$objExcel->getActiveSheet()->setTitle('mySheet2');
$objExcel->setActiveSheetIndex(0);
$outputFileName = "output.xls";
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$outputFileName.'"');
header("Content-Transfer-Encoding: binary");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');