PHPExcel how to link a sheet column to a cell using setFormula1()

时间:2016-04-25 09:26:41

标签: php excel phpexcel

I am using a PHPExcel for xls creation. how can i link a sheet column to a drop down cell. i have created a drop down in xls but how can i get sheet column like whole A column in drop down for drop down values. In excel i have done this manually when i link a sheet column to any cell its making formula like this

=DropdownSheet!$A:$A

DropdonSheet is a sheet name A is a column that i am linking

I have build a sheet using PHPExcel exactly i have done in excel DropdonSheet when i write this formula in PHPExcel to populate dropdown

$objValidation->setFormula1("=DropdownSheet!$A:$A");

its not working giving me an error. what i am doing wrong can any one please guide me here is my whole code

    $objPHPExcel = new PHPExcel();

// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Something');
$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Something');
$objPHPExcel->getActiveSheet()->setCellValue('A3', 'Something');
$objPHPExcel->getActiveSheet()->setCellValue('A4', 'Something');
$objPHPExcel->getActiveSheet()->setCellValue('A5', 'Something');
$objPHPExcel->getActiveSheet()->setCellValue('A6', 'Something');
$objPHPExcel->getActiveSheet()->setCellValue('A7', 'Something');
$objPHPExcel->getActiveSheet()->setCellValue('A8', 'Something');
$objPHPExcel->getActiveSheet()->setCellValue('A9', 'Something');

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('DropdownSheet');

// Create a new worksheet, after the default sheet
$objPHPExcel->createSheet();

// Add some data to the second sheet, resembling some different data types
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'More data');
$objValidation = $objPHPExcel->getActiveSheet()->getCell("B1")->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("=DropdownSheet!$A:$A");

// Rename 2nd sheet
$objPHPExcel->getActiveSheet()->setTitle('Second sheet');

1 个答案:

答案 0 :(得分:1)

这是在PHPExcel中编写此公式的方法

 $objValidation->setFormula1('DropdownSheet!$A$2:$A$6');

// Make sure NOT to put a range of cells or a formula between " and "  !!!

DropdownSheet 是工作表名称

A 是列名

我发现这个解决方案,它的工作正常可能会帮助将来这里的任何人提供更多细节的链接 link