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.
Related
In PHP, is there a way to force the value "00123" to be inserted into a CSV file as a string?
This way, when you open the CSV file the value will remain 00123 rather than removing the leading zeros and showing 123.
The primary reason I'd like achieve this is for a list of zipcodes, whereas there are multiple zipcodes that have leading zeros and I'd like the values to reflect that.
<?php
if( $fh = fopen('filename.csv','w') ){
$line = ['00123'];
fputcsv($fh,$line);
fclose($fh);
}
CSV does not have types. Values written using the ,"..", syntax merely delimit the value to disambiguate the usage of , within the value itself; it does not mean that the value is "a string".
I suspect your values are mangled when imported into Excel or such. There's no solution to this that CSV can offer; you can only import the file using the import wizard and specify that the column should be used as is and not cast to a number. (This may or may not actually work depending on what effed-up version of Excel you're using.)
If you don't want to go through this every time, you should be producing an XLSX file, which does have types.
I guess there is no way to do it because "CSV" files are just "Comma-Separated Values"
You have to use the editor options for csv import.
$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'm using
\PHPExcel_Shared_String::setThousandsSeparator(',');
to define a thousand separator for Excel file. Thanks to it, a cell is displayed as 55 452 instead of 55452. Still, the cell value is an integer 55452, so it can be calculated, used within charts, etc.
Is there a similar way to define a million separator and so on (e.g. to define a separator each 3 digits)?
You need to define number format like this "#,#0.##;[Red]-#,#0.##"
$workbook = new Spreadsheet_Excel_Writer('example.xls');
$worksheet =& $workbook->addWorksheet("example_sheet_name");
$format =& $workbook->addFormat();
$format->setNumformat('#,#0.##;[Red]-#,#0.##');
$worksheet->writeNumber(1, 1, '100000',$format);
output : 100.000,00
A thousands separator will be used for millions, billions, etc; as far as I'm aware, Excel doesn't directly support different separators for each multiple
However, you could define a number format mask of:
#:###!###,##0.00
and it would probably work in MS Excel itself, though PHPExcel wouldn't format the value correctly
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.
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 !