Home » Php » php – Populate dropdown with phpexcel

php – Populate dropdown with phpexcel

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m running into the following problem.

Im trying to get some results from my database and I want to insert the results into a dropdown list.

In the examle file was the following example:

$objValidation->setFormula1('"Item A,Item B,Item C"');

so the results have to be comma seperated and the total results have to be between “”.

Here is the code so far:

$configurations = Db::getInstance()->queryResults('SELECT * FROM configurations', array($siteNumber));
$objPHPExcel->getActiveSheet()->setCellValue('B7', "List:");

$configs = '"';
foreach($configurations as $config) {
    $configs .= $config->configuration_name . ', ';
}
$configs .= '"';

$objValidation = $objPHPExcel->getActiveSheet()->getCell('B8')->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);  

Anybody has an idea why the dropdownlist does not get populated??

Thanks in advance!

Dump database results:

string(3100) "DUS800, DUG900+3xRRUS, DUW2100, 2xMU, SIU, DUS800+3xRRUS, DUG900+3xRRUS, DUW2100, 2xMU, SIU, DUS800+3xRRUS, DUG900+3xRRUS, DUW2100+3xRRUS, 2xMU, SIU, DUS800, DUG900+3xRRUS, DUW2100+3xRRUS, 2xMU, SIU, DUS800, DUS1800+3xRRUS, MU, SIU, DUS800+3xRRUS, DUS1800+3xRRUS, MU, SIU, DUS800, DUG900+3xRRUS, MU, SIU, DUS800+3xRRUS, DUG900+3xRRUS, MU, SIU, DUS800, MU, SIU, DUS800+3xRRUS, MU, SIU, DUS800, DUW2100, 2xMU, SIU, DUG900+3xRRUS, MU, DUS1800+3xRRUS, MU, SIU, DUW2100, MU, DUW2100+3xRRUS, MU, DUG900+3xRRUS, MU, DUG900+3xRRUS, DUW2100, 2xMU, DUG900+3xRRUS, DUW2100+3xRRUS, 2xMU, DUW2100, MU, DUW2100+3xRRUS, MU, RBS6102 DUS800+DUG900+DUW2100 outdoor+SIU, RBS6102hybrid DUS800+DUG900+DUW2100Remote outdoor+SIU, RBS6201 DUS800+DUG900+DUW2100 indoor+SIU, RBS6201hybrid DUS800+DUG900+DUW2100Remote indoor+SIU, RBS6102 DUS800+DUS1800 outdoor+SIU, RBS6201 DUS800+DUS1800 indoor+SIU, RBS6102 DUS800 outdoor+SIU, RBS6201 DUS800 indoor+SIU, RBS6102 DUS800+DUG900 outdoor+SIU, RBS6201 DUS800+DUG900 indoor+SIU, RBS6102 DUS1800 outdoor+SIU, RBS6201 DUS1800 indoor+SIU, RBS6102 DUG900+DUS1800 outdoor+SIU, RBS6201 DUG900+DUS1800 indoor+SIU, RBS6102 DUG900+DUW2100 outdoor, RBS6201 DUG900+DUW2100 indoor, RBS6102hybrid DUG900+DUW2100Remote outdoor, RBS6201hybrid DUG900+DUW2100Remote indoor, RBS6102 DUG900 outdoor, RBS6201 DUG900 indoor, RBS6102 DUW2100 outdoor, RBS6201 DUW2100 indoor, RBS6102 DUS1800+DUW2100 outdoor+SIU, RBS6201 DUS1800+DUW2100 indoor+SIU, Exp pakket G900 in RBS6102 UMTS, Exp pakket G900 in RBS6201 UMTS, Exp pakket G900 in RBS6102 LTE, Exp pakket G900 in RBS6201 LTE, Exp pakket U2100 in RBS6102 GSM, Exp pakket U2100 in RBS6201 GSM, Exp pakket U2100 in RBS6102 LTE, Exp pakket U2100 in RBS6201 LTE, Exp pakket U2100 in RBS6102 GSM/LTE, Exp pakket U2100 in RBS6201 GSM/LTE, Exp pakket U2100 OIL in RBS6102 GSM/LTE, Exp pakket L800 in RBS6102 GSM, Exp pakket L800 in RBS6201 GSM, Exp pakket L800 in RBS6102 LTE, Exp pakket L800 in RBS6201 LTE, Exp pakket L1800 in RBS6102 GSM, Exp pakket L1800 in RBS6201 GSM, Exp pakket L1800 in RBS6102 LTE, Exp pakket L1800 in RBS6201 LTE, expansie DUG1800+MU in Main-Remote, expansie DUG1800 in RBS6102, expansie DUG1800 in RBS6201, 3rd Carrier DUW2100 in bestaande Main-Remote met RBS6601 V1, 3th Carrier DUW2100 in bestaande Main-Remote met RBS6601 V2, 3rd Carrier DUW2100 in RBS6102, 3rd Carrier DUW2100 in RBS6201, Exp pakket add LTE1800 3x RRU, Exp pakket add LTE1800 3x RRU + 3x PSU 230V/48V, Exp pakket add LTE1800 3x RRU + 3x PSU 230V/48V + 1x DUS31, Exp pakket add LTE1800 3x RRU + 3x PSU 230V/48V + 1x DUS41, DU adapter frame t.b.v. RBS 6102, DU adapter frame t.b.v. RBS 6201, 3x DUMMY RUS t.b.v. ombouw macro naar OIL, 6x DUMMY RUS t.b.v. ombouw macro naar OIL, 1x PSU AC01 1,8kW  incl bekabeling, 4x PSU AC03 2,5kW incl bekabeling, Battery set complete 1x RBS 6102, Battery set complete 2x RBS 6102, Battery set complete 1x MMC, Battery set complete 2x MMC, Battery set complete 1x RBS6102+1x MMC, Battery set complete 2x RBS6102+1x MMC, Battery set complete indoor 1 set, Battery set complete indoor 2 sets, Battery set complete indoor 3 sets"
How to&Answers:

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.

Answer:

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."'");