Always display specified number of decimal places in Excel

$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('0.00'); 

should work, as long as the value in cell A1 is a number and not a formatted string... don't try to force Excel formatting by using sprintf, simply use the format codes.


Formatted as number with comma. Assuming that value is not exceeded by 9 digits.

$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('###,###,###.##');

You can also do this:

$sheet->setCellValue("A1",sprintf("%0.2f",$row['price']),true)->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);

Adding 'true' at setCellValue, you refer directly to the cell instead of the worksheet and you can call the getStyle method without the cell value.

Tags:

Php

Phpexcel