I am using PHPExcel to read some data from an xls
file.
I want to get a couple of cells at once, say: A6 - A11.
I know I can use $cell = $objPHPExcel->setActiveSheetIndex(0)->getCell('A6');
to get a single cell, and I could probably loop through an array and get each cell in my range.
But, isn't there a simpler method to get a range of cells something like getCellRange('A6:A11')
?
To retrieve the value of a cell, the cell should first be retrieved from the worksheet using the getCell() method. A cell's value can be read again using the following line of code: $objPHPExcel->getActiveSheet()->getCell('B8')->getValue(); If you need the calculated value of a cell, use the following code.
Use composer to install PhpSpreadsheet into your project. Or also download the documentation and samples if you plan to use them. A good way to get started is to run some of the samples. Don't forget to download them via --prefer-source composer flag.
Read an Excel File (XLSX) Open the XSLX file with $reader->open($path) Browse each Sheet of the spreadsheet with $reader->getSheetIterator() Browse each Row of the Sheet with $sheet->getRowIterator() Browse each Cell of the Row with $row->getCells()
There is, the rangeToArray()
method:
$objPHPExcel->setActiveSheetIndex(0)->rangeToArray('A1:C3');
Wondering why I bother documenting these methods in the first place, but here's the argument list as well:
/**
* Create array from a range of cells
*
* @param string $pRange Range of cells (i.e. "A1:B10"),
* or just one cell (i.e. "A1")
* @param mixed $nullValue Value returned in the array entry
* if a cell doesn't exist
* @param boolean $calculateFormulas Should formulas be calculated?
* @param boolean $formatData Should formatting be applied to cell values?
* @param boolean $returnCellRef False - Return a simple array of rows
* and columns indexed by number counting
* from zero
* True - Return rows and columns indexed by
* their actual row and column IDs
* @return array
*/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With