Populate dropdown with phpexcel

Here is the correctly working code:

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

require_once dirname(__FILE__) . './PHPExcel.php';


echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()
    ->setCellValue('B5', "SELECT ITEM")
    ;


$configs = "DUS800, DUG900+3xRRUS, DUW2100, 2xMU, SIU, DUS800+3xRRUS, DUG900+3xRRUS, DUW2100";

$objValidation = $objPHPExcel->getActiveSheet()->getCell('B5')->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('"'.$configs.'"');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Save Excel 95 file
echo date('H:i:s') , " Write to Excel5 format" , EOL;
$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('populate.xls');
?>

It will output to populate.php in the same directory as this script.

First off, it's not setFormula1("'".$configs."'"). It's setFormula1('"'.$configs.'"').

Secondly, you might be missing something elsewhere, which is why you are getting ERR_CONNECTION_RESET. I posted a working example just in case you are missing something else in the code as well. If you had posted the entire code, I could have known for sure.


Change the line

$objValidation->setFormula1($configs);  

to

$objValidation->setFormula1("'".$configs."'");  

Because in the structure the data is with in single quotes(').

Sample working code

$objValidation2 = $sheet -> getCell('E1') -> getDataValidation();
$objValidation2 -> setType(PHPExcel_Cell_DataValidation::TYPE_LIST);
$objValidation2 -> setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION);
$objValidation2 -> setAllowBlank(true);
$objValidation2 -> setShowInputMessage(true);
$objValidation2 -> setShowErrorMessage(true);
$objValidation2 -> setShowDropDown(true);
$objValidation2 -> setErrorTitle('Invalid date');
$objValidation2 -> setError('Date is not in list.');
$objValidation2 -> setPromptTitle('Select DOB date');
$objValidation2 -> setPrompt('Please pick a date from the drop-down list.');
$dates = '"01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31"';
$objValidation2 -> setFormula1("'".$dates."'");