Question

I want to display unit numbers in front of Property name. I fetched my data in $arrobjPropertyName array object and in foreach loop I print it as

foreach( $arrobjPropertyName as $objProperty ) {
    $objWriteExcelWorksheet->write( $intRowCount, $intColumnCount++, $objProperty->getUnitNumber(), $objWorkbookContent );}

it will give the output 613 or 722 but i want it like 0613 or 0722 in excel record.

Please any suggestions are welcomed.

Was it helpful?

Solution

foreach( $arrobjPropertyName as $objProperty ) {
    $objWriteExcelWorksheet->write( $intRowCount, $intColumnCount++, "'" . $objProperty->getUnitNumber(), $objWorkbookContent );}

you should tell excell it is a string. otherwise it will interpret as number and remove all leading 0s.

To be more elaborate: if you type 0123 in excell manually, it will auto convert to 123. If you type '0123 in excell manually it will stay like 0123. So not only php has to know it is a string (so that php doens't remove the 0. but you must also tell excell excplicitly.

Update: sorry, forgot, excell only needs first '

OTHER TIPS

Try this:

foreach( $arrobjPropertyName as $objProperty ) {
    $newNumber='0'.$objProperty->getUnitNumber();
    $objWriteExcelWorksheet->write( $intRowCount, $intColumnCount++,(string)$newNumber, $objWorkbookContent );
}

Read Type Juggling http://php.net/manual/en/language.types.type-juggling.php

You can generate fixed width number strings with sprintf.

$textnum = sprintf("%04d", $objProperty->getUnitNumber());

sprintf can also help encapsulating the number in quotes, if that is necessary for it to be recognized as a string:

$textnum = sprintf("'%04d'", $objProperty->getUnitNumber());

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top