使用 phpexcel 填充下拉列表
Populate dropdown with phpexcel
我 运行 遇到以下问题。
我正在尝试从我的数据库中获取一些结果,我想将这些结果插入到下拉列表中。
examle 文件中有以下示例:
$objValidation->setFormula1('"Item A,Item B,Item C"');
所以结果必须用逗号分隔,总结果必须在“”之间。
目前的代码如下:
$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);
有人知道为什么下拉列表没有填充吗??
提前致谢!
转储数据库结果:
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"
换行
$objValidation->setFormula1($configs);
到
$objValidation->setFormula1("'".$configs."'");
因为在结构中数据是用单引号(')括起来的。
示例工作代码
$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."'");
这是正确的工作代码:
<?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');
?>
它将输出到与此脚本相同的目录中的populate.php
。
首先,这不是 setFormula1("'".$configs."'")
。这是 setFormula1('"'.$configs.'"')
.
其次,您可能在其他地方遗漏了一些东西,这就是您得到 ERR_CONNECTION_RESET
的原因。我发布了一个工作示例,以防万一您在代码中也遗漏了其他内容。如果您发布了完整的代码,我就可以确定了。
我 运行 遇到以下问题。
我正在尝试从我的数据库中获取一些结果,我想将这些结果插入到下拉列表中。
examle 文件中有以下示例:
$objValidation->setFormula1('"Item A,Item B,Item C"');
所以结果必须用逗号分隔,总结果必须在“”之间。
目前的代码如下:
$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);
有人知道为什么下拉列表没有填充吗??
提前致谢!
转储数据库结果:
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"
换行
$objValidation->setFormula1($configs);
到
$objValidation->setFormula1("'".$configs."'");
因为在结构中数据是用单引号(')括起来的。
示例工作代码
$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."'");
这是正确的工作代码:
<?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');
?>
它将输出到与此脚本相同的目录中的populate.php
。
首先,这不是 setFormula1("'".$configs."'")
。这是 setFormula1('"'.$configs.'"')
.
其次,您可能在其他地方遗漏了一些东西,这就是您得到 ERR_CONNECTION_RESET
的原因。我发布了一个工作示例,以防万一您在代码中也遗漏了其他内容。如果您发布了完整的代码,我就可以确定了。