Read the list of options in a drop-down list - Phpexcel

徘徊边缘 提交于 2019-12-10 21:09:58

问题


I want to read the list of options that are given within a drop-down in an excel sheet.

For example: - There is a cell(86,G) that has Not Attempted as an option but it is a drop down list. So no matter what the user selects i want to read the last option from the drop-down.

How to achieve this?

Thanks


回答1:


Follow the below Steps to read drop down in PHPExcel

Step 1: Store the drop down data in separate sheet

Step 2: Create Drop down in separate sheet

Refer the below code.

$newSheet=$objPHPExcel->createSheet();            
$objPHPExcel->setActiveSheetIndex(1);
$newSheet->setTitle("CountriesList");

$objPHPExcel->setActiveSheetIndex(1)
->SetCellValue("A1", "UK")
->SetCellValue("A2", "USA")
->SetCellValue("A3", "CANADA")
->SetCellValue("A4", "INDIA")
->SetCellValue("A5", "POLAND")
->SetCellValue("A6", "ENGLAND");// Drop down data in sheet 1

$objPHPExcel->addNamedRange( 
new PHPExcel_NamedRange(
'countries', 
$objPHPExcel->setActiveSheetIndex(1), 
'A1:A6'
) 
);
$objPHPExcel->setActiveSheetIndex(0)->SetCellValue("A1", "UK");

$objPHPExcel->setActiveSheetIndex(0);// Drop down in sheet 0
$objValidation = $objPHPExcel->getSheet(0)->getCell('A1')->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->setFormula1("=countries");

These steps should be followed on creating xls.

When you going to read drop down use data stored in second sheet instead of drop down.



来源:https://stackoverflow.com/questions/18377976/read-the-list-of-options-in-a-drop-down-list-phpexcel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!