Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a range of cells in PHPExcel?

Tags:

php

phpexcel

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') ?

like image 231
hitautodestruct Avatar asked Aug 13 '13 07:08

hitautodestruct


People also ask

How to get cell value in PHPExcel?

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.

How do I download PhpSpreadsheet?

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.

How can I open XLSX file in PHP?

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()


1 Answers

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
 */
like image 150
Mark Baker Avatar answered Oct 13 '22 07:10

Mark Baker