I need to display two decimal places in Excel (xlsx). I'm using PHPExcel. In PHP, I can use something like the following to display only the specified number of decimal paces.
echo sprintf("%0.2f", $row['price']);
which always displays two decimal places even though the value contains no decimal point i.e if the value of $row['price'] is 1500, it will echo 1500.00. I need to write the same value to excel. I tried
$sheet->setCellValue("A1", sprintf("%0.2f", $row['price']));
but it displays 1500 instead of displaying 1500.00 (because Excel automatically assumes it to be a numeric value and the part after the decimal point i.e .00 in this case is truncated).
I also tried the following
$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('0.00');
but it's not sufficient to achieve what is specified.
How can I (always) display the specified number of decimal places in Excel using PHPExcel?
$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.
If you want some special characters just use html_entity_decode:
$currencyFormat = html_entity_decode("€ 0,0.00",ENT_QUOTES,'UTF-8');
$objPHPExcel->getActiveSheet()
->getStyle("A1")
->getNumberFormat()->setFormatCode($currencyFormat);
$objPHPExcel->getActiveSheet()->getStyle('ce')->getNumberFormat()->setFormatCode('0.00');
Try that:
$sheet->getStyle("A1")->getNumberFormat()->setFormatCode('### ### ### ##0.00');
it works.
Related
I am using matwebsite for exporting excel files ,i am getting the data from the database it's stored in the exported excel file upto this part it's working fine .some of the values are more than 15 digits at that time it's displaying as scientifc format for that i made some changes using columnFormats() function now it supports upto 16 digits ,if any value more than 16 digits it's replacing last digits as zero's ,How to rectify this error please help me to fix this issue..
``
public function columnFormats(): array
{
return [
'D' => '#0',
];
}
**my downloaded excel**
1234567890123456 //16 digits it's working fine
12345678901234567000 //actual value is 1234567890123456789
Columnformats function tells the excel to convert to a numeric behaviour in Excel the numeric supports upto 16 digits so this change is not working in your case.
If you want to acheive your scenario use this following reference bindValue() function and change the data type as string(string in Excel supports more characters).
https://docs.laravel-excel.com/3.1/imports/custom-formatting-values.html
I'm importing a CSV directly into my database via PHPmyadmin. Everything has gone smoothly until I checked a column that has decimal spaces. Instead of keeping the decimal places in tact it simply removed everything after the ', & .'.
I tried removing the separators before uploading and then it just shows the full number i.e. 125657458 and doesn't add in the decimals.
I have set the Datatype to 'DECIMAL' for the column.
Row From CSV:
Ricky Manager Standard Normal 156,200
Results with decimal place in-tact on upload:
Ricky Manager Standard Normal 156
Results with decimal place not present on upload:
Ricky Manager Standard Normal 156200
DECIMAL(10,0) says to round to an integer. So, 156.200 becomes 156 and 156.9 becomes 160. Do you have any examples to verify the latter?
I notice you used "," as the decimal point; have you configure things to the call "," a decimal point and "." a 'thousands separator'? That is not the default.
DECIMAL(10,3) will take in 156.789 exactly.
No datatype will accept input that has a 'thousands separator'. That is, nothing will correctly accept 123,4567,789.12 (English) or 123.456.789,12 (some European) or 12,34,567.89 (Indian).
$this->excel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode('0.0000');
$this->excel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
If I try this code the code to display '$' in the cell would work but not the decimal one, if i reverse it then code to display four decimal points would work but not the code to display '$'.
I am trying to write into excel file using PHPExcel library
Number format codes are simply strings, you don't have to use the built-in values; but can set them to any valid MS Excel number format mask
e.g
$this->excel->getActiveSheet()->getStyle('A1')
->getNumberFormat()
->setFormatCode('$ #,##0.0000');
You can even be really clever with them
$this->excel->getActiveSheet()->getStyle('A1')
->getNumberFormat()
->setFormatCode('$ #,##0.0000;[Red]-$ #,##0.0000');
I am converting a PHP script into excel sheet using PHPExcel library I want comma separated values without floating number (.00).
I am using following script:
$objPHPExcel->getActiveSheet()->getStyle("A1")->getNumberFormat()->setFormatCode
(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
$objPHPExcel->getActiveSheet()->setCellValueExplicit("A1",(1111111),
PHPExcel_Cell_DataType::TYPE_NUMERIC);
But when I convert the file in PHPExcel result show 1,111,111.00 but I want is 1,111,111 .
Is there any way?
Just specify the right format string ('#,##0') by hand:
objPHPExcel->getActiveSheet()->getStyle("A1")->getNumberFormat()->setFormatCode('#,##0');
From the PHPExcel documentation:
FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00'
This has two decimal places after the point, which you don't need.
While writing the excel file is fine I see that really long numbers are formulas in excel
Example: 8.71129E+12
instead of: 1234567890
How can I change the format during the PHP Excel Creation?
I'm following the simple example here
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', '1234567890', PHPExcel_Cell_DataType::TYPE_STRING);
Either set the value explicitly as a string, or set a number format mask for the cell that forces display of all digits (e.g. '#,##0' 0r '0') rather than default format.
Try this code it's works for me :
$objPHPExcel
->getActiveSheet()
->getCellByColumnAndRow($col, $row)
->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
And Good luck !