我需要使用单独的工作表在Excel中创建一个下拉列表。
// Creating original sheet.
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
// Creating auxiliary sheet for drop down data.
$objWorkSheet = $objPHPExcel->createSheet(0);
$objWorkSheet->setTitle("autofill");
$objWorkSheet->setCellValue('A1', 2)
->setCellValue('A2', 3);
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B2')->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.');
// Setting the formula in original sheet.
$objValidation->setFormula1("='autofill'!$A$1:$A$2");
我在B2单元格中下拉,但只有一个值为0(如图所示)。
有人可以指出代码中的错误吗?
答案 0 :(得分:0)
Hello_ mate。
我认为您的代码问题就是这一行代码:
$objValidation->setFormula1("='autofill'!$A$1:$A$2");
尝试将其更改为
$objValidation->setFormula1('=\'autofill\'!$A$1:$A$2');
这是因为当您使用双引号时PHP
将$A
视为变量......
祝你好运,如果这修复了您的代码,请告诉我
修改强>
实际上我现在测试了你的代码,似乎工作正常。如果您没有像我告诉您的那样更改代码,那么只有 NOTICE ,但代码仍然有效。我看到两张纸(自动填充和工作表),第二张在 B2 下拉,值为2,3(来自自动填充表)。因此,请尝试在代码的其他部分中搜索问题或与我们分享,我们可以看到问题出在哪里。
我测试过的代码段(与您的代码相同):
$objPHPExcel = new \PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
// Creating auxiliary sheet for drop down data.
$objWorkSheet = $objPHPExcel->createSheet(0);
$objWorkSheet->setTitle("autofill");
$objWorkSheet->setCellValue('A1', 2)
->setCellValue('A2', 3);
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B2')->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.');
// Setting the formula in original sheet.
$objValidation->setFormula1('=\'autofill\'!$A$1:$A$2');
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('test.xlsx');