Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct format for strings / numbers beginning with zero?

Tags:

php

phpexcel

I'm trying to use PHP to create a file containing a list of phone numbers. It's working OK however if the phone number begins with zero, the digit is dropped from the Excel file.

Does anyone know how to set the formatting correctly so that it remains in place?

like image 323
Dan Avatar asked Jun 16 '10 14:06

Dan


People also ask

How can I format a number into a string with leading zeros?

To pad an integer with leading zeros to a specific length To display the integer as a decimal value, call its ToString(String) method, and pass the string "Dn" as the value of the format parameter, where n represents the minimum length of the string.

What is a leading zero example?

A leading zero is any 0 digit that comes before the first nonzero digit in a number string in positional notation. For example, James Bond's famous identifier, 007, has two leading zeros. When leading zeros occupy the most significant digits of an integer, they could be left blank or omitted for the same numeric value.

What is the format of string?

String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text. As a data scientist, you would use it for inserting a title in a graph, show a message or an error, or pass a statement to a function.

How do you format numbers by Prepending 0 to single digit numbers in Python?

zfill() Function to Display a Number With Leading Zeros in Python. The str. zfill(width) function is utilized to return the numeric string; its zeros are automatically filled at the left side of the given width , which is the sole attribute that the function takes.


3 Answers

Either:

// 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'); 
like image 199
Mark Baker Avatar answered Sep 18 '22 05:09

Mark Baker


Set the type to string explicitly:

$type = PHPExcel_Cell_DataType::TYPE_STRING;
$sheet->getCellByColumnAndRow($column, $rowno)->setValueExplicit($value, $type);
like image 22
Sjoerd Avatar answered Sep 18 '22 05:09

Sjoerd


This is an old question but I was recently struggling with this issue and I thought it may help someone in the future if I post some additional info here:

Whilst the above answers are correct, the formatting gets lost when you remove a column or row that is located before the formatted cell.

The solution that seems to be resistand to that is:

$cellRichText = new \PHPExcel_RichText($worksheet->getCell($cell));                        
$cellRichText->createText($cellValue);
like image 37
Tomasz Avatar answered Sep 20 '22 05:09

Tomasz