20

I would think that a getCell($X, $y) or getCellValue($X, $y) would be available for one to easily pick a a certain value. This can be usefully, as example crosscheck data prior to a larger process.

How do you get a specific value from say cell C3.

I do not want an array of values to sort through.

kero
  • 10,647
  • 5
  • 41
  • 51
CrandellWS
  • 2,708
  • 5
  • 49
  • 111

3 Answers3

62

Section 4.5.2 of the developer documentation

Retrieving a cell by coordinate

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

Section 4.5.4 of the developer documentation

Retrieving a cell by column and row

To retrieve the value of a cell, the cell should first be retrieved from the worksheet using the getCellByColumnAndRow method. A cell’s value can be read again using the following line of code:

// Get cell B8
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, 8)->getValue();

If you need the calculated value of a cell, use the following code. This is further explained in 4.4.35

// Get cell B8
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, 8)->getCalculatedValue();
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • I was thinking an array of data was returned with the `getValue()` method... so i was wanting to use the `getFormattedValue()` method, as it did return only what I wanted. I will test again and choose an answer, improve my answer or determine if i need to better state my question. Thanks for the well formatted answer btw. – CrandellWS Mar 26 '14 at 14:27
8

By far the simplest - and it uses normal Excel co-ordinates:

// Assuming $sheet is a PHPExcel_Worksheet

$value = $sheet->getCell( 'A1' )->getValue();

You can separate the co-ordinates out in a function if you like:

function getCell( PHPExcel_Worksheet $sheet, /* string */ $x = 'A', /* int */ $y = 1 ) {

    return $sheet->getCell( $x . $y );

}

// eg:
getCell( $sheet, 'B', 2 )->getValue();
Brian North
  • 1,398
  • 1
  • 14
  • 19
1

This is a source based answer feel free to improve or comment.

function toNumber($dest)
{
    if ($dest)
        return ord(strtolower($dest)) - 96;
    else
        return 0;
}

function myFunction($s,$x,$y){
 $x = toNumber($x);
 return $s->getCellByColumnAndRow($x, $y)->getFormattedValue();
}


$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->setActiveSheetIndex(0);
$sheetData = $objPHPExcel->getActiveSheet();


$cellData = myFunction($sheetData,'B','2');
var_dump($cellData);

This does not work past the letter Z, and could be improved but works for my needs.

CrandellWS
  • 2,708
  • 5
  • 49
  • 111
  • 5
    It could work for the full MS Excel column range (not simply A-Z) if you used PHPExcel's own built-in `PHPExcel_Cell::columnIndexFromString()` method rather than your own `toNumber()` – Mark Baker Mar 22 '14 at 09:54