phpexcel thousand separator for xlsx - php

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

Related

Force string data type when writing to CSV file

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.

How to have Number format to display '$' as well as decimals of 4 places in phpexcel

$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');

Comma separated values show with out float(.00) number in PHPExcel

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.

phpExcel write long number in cell

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 !

Excel BIFF file format - cell type for date

I'm trying to write an excel file from php, and I need that some cells to have date type; do you have any ideas which is the format code for that type of cell?
I'm using the code from http://pizzaseo.com/php-excel-creator-class to generate the excel files.
Thanks.
Your question is a little vague, and I know little about PHP, but I'm the maintainer of a similar Python package ... so here's an attempt to help:
Excel doesn't differentiate at the BIFF record level between numbers and dates. The only way that an XLS reader can tell whether a date was intended by the writer is to parse the "number format" that the writer associated (indirectly) with the cell in question.
I presume that you are using this Format function to set the num_format of what the package calls a "format" (which includes not only "number format" but alignment, background, borders, ...):
function setNumFormat($num_format)
$num_format should be an Excel "number format" (e.g. for a money amount you might use "$0.00"). For a date you just use whichever you prefer of the standard date formats Excel provides, or you could use a customised format like "yyyy-mm-dd hh:mm:ss" (handy when debugging).
Then you do:
worksheet->write($row, $col, $value, $format)
Where $format is as described above. For a date, $value should be a floating point day-number where 1900-03-01T00:00:00 is represented as day-number 61.0 and you should avoid earlier dates because ... [long rant omitted], and if there's a "datemode" or "1904" option available, ensure that you are using the (should be default) 0 or False or "no, thanks" option.
http://www.codeplex.com/PHPExcel might be mopre complete solution.
Here's the reference document for the Excel binary format from Microsoft, this gives you all the information regarding the various cell types:
Microsoft Excel 97-2007 Binary File Format

Categories