Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel How to get only 1 cell value?

Tags:

php

phpexcel

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.

like image 477
CrandellWS Avatar asked Mar 21 '14 18:03

CrandellWS


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

What is PhpSpreadsheet?

PhpSpreadsheet is a library written in pure PHP and offers a set of classes that allow you to read and write various spreadsheet file formats such as Excel and LibreOffice Calc.


2 Answers

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();
like image 149
Mark Baker Avatar answered Sep 22 '22 07:09

Mark Baker


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();
like image 25
Brian North Avatar answered Sep 26 '22 07:09

Brian North