php - 使用 phpexcel 填充下拉列表

标签 php excel 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) "item1, item2, item3 etc etc"

最佳答案

这是正确工作的代码:

<?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 的原因。我发布了一个工作示例,以防万一您在代码中也遗漏了其他内容。如果您发布了完整的代码,我就可以确定了。

关于php - 使用 phpexcel 填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38185924/

相关文章:

php - 在php中的数据库中设置 session

php - 在我的 linux 服务器上,我收到一个 php 错误,但我看不到错误,为什么?

php - 将 html select 的 mysqli 查询与当前选择相结合

php - 如何使用特定版本的curl和openssl编译php?

php - 当尝试更新记录时,它给出的值为 0 - codeigniter

excel - Powerpoint VBA - 在嵌入式 Excel OLE 对象中编辑表的列名

vba - 在 VBA 中关闭/释放 Word 对象?

Excel 匹配,索引 VLOOKUP 多个条件

php - 在 php 中读取 xls 日期

php - 使用 PHPExcel 数组到 Excel 2007