Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Cell as String in PHPExcel by column and row

I am trying to read a cell with possible trailing zeros as a string instead of numeric (which strips off leading zeros). The cell is read by integer column/row as below instead of column string as this answer has.

initial code

    $instReader = $reader->load($this->file);
    $sheet = $instReader->getSheet(0);

I tried modifying this from:

$keyCell = $sheet->getCellByColumnAndRow(1,5);

to:

$sheet->setCellValueExplicitByColumnAndRow(1,5, PHPExcel_Cell_DataType::TYPE_STRING);
$keyCell = $sheet->getCellByColumnAndRow(1,5);

the former gives 1407 for $keyCell instead of 01407

the latter gives "s" or ""

how do I treat the cell as string before calling getCellByColumnAndRow and using only integer values for column and row.

(BTW, if this can be done once for an entire column instead of each time for each individual cell that would be better)

like image 252
user813801 Avatar asked Jan 25 '26 17:01

user813801


1 Answers

$keyCell = $sheet->getCellByColumnAndRow(1,5)->getValue();

Will read the cell data in the format that it's actually stored by Excel, you can't arbitrarily change that or tell PHPExcel to read it as a different datatype.

However, if the cell has formatting applied, then you can use

$keyCell = $sheet->getCellByColumnAndRow(1,5)->getFormattedValue();

instead, and this will return the data as a string, with whatever format mask was defined in the Excel spreadsheet

like image 74
Mark Baker Avatar answered Jan 28 '26 09:01

Mark Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!