简体   繁体   中英

How to read a downdown values in an Excel using PHPExcel

I have a excel file where one column has a dropdown values.I am using PhpExcel so i need to read the dropdown values from it.How can this be achieved.

when i am reading the rows as

$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);

I m just getting the text when i dump the row i need the dropdown values too.

Any help will be appreciated.

If you need all the values from a cell dropdown, not simply the selected value, then you have to read the dropdown itself. This is a datavalidation object.

Assuming that the dropdown is in cell B1:

$objValidation = $objPHPExcel->getActiveSheet()
    ->getCell('B1')
    ->getDataValidation();

will return the data validation object

You can then read the rules for that data validation using methods like getType() , getOperator() , getFormula1() and getFormula2() .... note that not all data validations are dropdowns; you'd be looking for a type of PHPExcel_Cell_DataValidation::TYPE_LIST for a dropdown list. You might also need to use the calculation engine to evaluate the formula used to build the list if it was derived from data elsewhere in the spreadsheet.


Alternatively, because it's unclear from your question, it could be a autofilter dropdown that you're asking about. You can retrieve the autofilter object for a worksheet using:

$autoFilter = $objPHPExcel->getActiveSheet()
    ->getAutoFilter();

Again, there are a series of methods for accessing the filter rules for each column.

$autoFilter->getColumn('C');

Will return the autofilter rules for column C , with methods like getFilterType() and getRules() providing the rule definitions for that autofilter.

The autofilter examples like 10autofilter-selection-display.php show how to define autofilters; but there is also a useful showHideRows() method that can be used the filter to hide/display the rows that match those rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM