Numbers as Strings

Topics: Developer Forum, User Forum
Oct 19, 2010 at 2:17 PM


I'd like to store numbers as strings. But when I do this:

$objPHPExcel->getActiveSheet()->setCellValue($fieldName, "00120");

The cell is set to "120". I've tried:


but that doesn't seem to work either. It seems like


would be a good solution. But the length of the string can fluctuate (and this has to work for a client, who is tech illiterate). For example, there's a chance that when the client downloads the XLSX file, they'll want to change the string from a 5 digit to a 6 digit number, and then they'll blame me that the XLSX isn't letting them (if their number has leading zeros) ...

I've been reading through the documentation, and I've searched the discussions, but I don't see a solution. Any ideas? I see that in Excel, the cell's formula is '00120 (with a leading '). Is this the solution? How do I do it?



Oct 20, 2010 at 3:41 PM

I was able to find it in the developer documentation, section 4.6.7.

$objPHPExcel->getActiveSheet()->getCell($fieldName)->setValueExplicit('00120', PHPExcel_Cell_DataType::TYPE_STRING);
Oct 22, 2010 at 8:29 AM

Glad you found a solution that works for you.

Oct 27, 2010 at 2:22 PM


your answer helped me, but I would use

$sheet->setCellValueExplicitByColumnAndRow($pColumn, $pRow, $pValue, PHPExcel_Cell_DataType::TYPE_STRING);


and transform everything to string