¿Cómo hacer que phpexcel mantenga ceros delante de los números de teléfono?

Here's the code i'm using right now to set the cell values. It works alright if the number has separators like . or / but when there's no separator it gets saved as int and the leading 0 is stripped

$sheet->setCellValue($letter[$j].$row_nr,$entity['Phone'], PHPExcel_Cell_DataType::TYPE_STRING);

preguntado el 09 de marzo de 12 a las 13:03

6 Respuestas

Ya sea:

// Set the value explicitly as a string
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', '0029', PHPExcel_Cell_DataType::TYPE_STRING);

or

// Set the value as a number formatted with leading zeroes
$objPHPExcel->getActiveSheet()->setCellValue('A3', 29);
$objPHPExcel->getActiveSheet()->getStyle('A3')->getNumberFormat()->setFormatCode('0000');

Note that in the first case I'm calling the setCellValueExplicit() method, not the setCellValue() method. In your code, passing PHPExcel_Cell_DataType::TYPE_STRING to setCellValue() has no meaning, and the argument is simply ignored.

respondido 09 mar '12, 14:03

Is there anyway to specify the data type when exporting the data sin explictly providing the cell name (e.g. A3)? - crmpicco

@crmpicco - Don't understand.... if you're setting a cell value, without specifying a cell name, how is PHPExcel supposed to know what cell value you want to set? - Mark Baker

Apologies if this is slightly off-topic, but I am looking for a PHP solution that will allow me to export data from an array that includes elements such as 0078. MS Excel converts this value to 78 which is invalid for the application that I wish to use it in, so I am looking for a 3rd party solution to alleviate this problem. PHPExcel has came up in my searches, but looking at the documentation it would seem I can't export the data and sate that it is a string and not an integer? Unless I have to specify the cell name - is that correct? - crmpicco

PHPExcel uses a "value binder" to control how data is stored in a cell. The default behaviour is to identify the datatype based on PHP's is_numeric(), and treat it as a number, converting as apprpriate; but you can write your own binder to define specific behaviours. You could also set a number format mask to display the numeric with leading zeroes, and that can be set for a range of cells, or even a whole worksheet - Mark Baker

The easiest solution is to use setCellValueExplicitByColumnAndRow($pColumn = 0, $pRow = 1, $pValue = null, $pDataType = PHPExcel_Cell_DataType::TYPE_STRING),

$PHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($columnPointer, $rowPointer, $value);

contestado el 15 de mayo de 13 a las 16:05

For me this did the trick

// Set the value explicitly as a string
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', '0029', PHPExcel_Cell_DataType::TYPE_STRING);

Respondido 26 ago 15, 21:08

Just came across an alternative solution and I thought I'd post it here. This does not require the use of libraries, just formatting the required .xls cells when creating the html table to be exported.

<td style="mso-number-format:'@';">your number w/leading zeroes here</td>

I hope someone finds it useful. Below is a complete reference with formatting codes:

http://www.ozgrid.com/Excel/CustomFormats.htm

Respondido 04 ago 15, 19:08

Whenever someone proceeds like I did, this can help :

$inputFileName = 'file.xls';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$objWorkSheet = $objPHPExcel->getActiveSheet();
$objWorkSheet->getCell('A1')->setValueExplicit('0029', PHPExcel_Cell_DataType::TYPE_STRING);

As inspired by esta respuesta. I hope this help somebody.

Respondido 05 Jul 17, 12:07

I came across this thread when looking for a solution and there is my other answer that may be helpfull for someone in case or column/row deletion that causes cell formatting to get lost...

contestado el 23 de mayo de 17 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.