phpExcel write long number in cell - php

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 !

Related

Store large number in a cell with PHPExcel

I need to store a 24 digits number in a cell with PHPExcel.
I've read some tutorials about that. But the problem still exists...
Here is the code:
$objPHPExcel->getActiveSheet()->getStyle('A2')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$objPHPExcel->getActiveSheet()->SetCellValue('A2', '680540101947000270348604');
$objPHPExcel->getActiveSheet()->SetCellValue('C2', '100');
It supposed to store entire number without any change. But when I open file it's stored like:
6.80540101947E23
I tried also to save that as an string and that worked. But I have to deliver this file to bank so their system is so stupid and does't accept number as string...They need to calculate I guess.
Any idea?
This worked for me:
// Set the value explicitly as a string
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A2', '680540101947000270348604', PHPExcel_Cell_DataType::TYPE_STRING);
First you need to set cell format as text then it will be possible.
$objPHPExcel
->getActiveSheet()
->getStyle('A')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

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

Formula disappear php excel

I'm trying to use a formula which is not implanted in PHPExcel but in excel, COUNTIFS whit 2 arguments.
I update the formula file in phpexcel and I add
$objWriter->setPreCalculateFormulas(false);
To have no generation errors.
But when I open the file with excel I only have a 0 and no formula. I also have an open and repair and this remove the function I think
This is for example
$sheet->setCellValue($this->intToChar($j).($end+$i),'=COUNTIFS(E$17:F$47;$B58;E$16:F$46;$B58)');
If I just add
$sheet->setCellValue($this->intToChar($j).($end+$i),'COUNTIFS(E$17:F$47;$B58;E$16:F$46;$B58)');
and I set an = in excel it works
Thanks for your help
The documentation explicitly states that commas (,) should be used as an argument separator for function arguments, unless you've explicitly set a locale for the calculation engine; so you need
$sheet->setCellValue(
$this->intToChar($j).($end+$i),
'=COUNTIFS(E$17:F$47,$B58,E$16:F$46,$B58)'
);
And why are you using a homebrew intToChar() function when there's a built-in PHPExcel_Cell::stringFromColumnIndex() function?

Formatting a cell to a percentage in PHPExcel

I am automating an excel spreadsheet using PHP.
I have been looking for a way to pragmatically format a cell to a percentage in PHPExcel.
I want to change a value like
0.077922078
to
8%
Is there a solution for this?
Thanks in advance.
assuming your cell is A1 ..
$objPHPExcel->getActiveSheet()->getStyle('A1')
->getNumberFormat()->applyFromArray(
array(
'code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00
)
);
PHPExcel library has predefined only few basic formatting constants. You can actually build your own for virtually any purpose (coloring, formatting decimals & thousands etc). Formatting capabilities in Excel are huge. Following will format percent with 3 decimal places and coloring negative values to red:
$workSheet
->getStyleByColumnAndRow($column, $row)
->getNumberFormat()
->setFormatCode('0.000%;[Red]-0.000%');
You can try this code:
$colLetter = "A";
$rowNumber = "1";
$objPHPExcel->getActiveSheet()
->getStyle("$colLetter:$rowNumber")
->getNumberFormat()
->applyFromArray([
"code" => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE
]);
Just in case someone else is seeing this after PHPExcel turns to PHPSpreadsheet.
PHPSpreadsheet have a dedicated function to apply format codes. So, to set a number as percentage now we need to replace applyFromArray() to setFormatCode() as follow:
$spreadsheet
->getActiveSheet()
->getStyle("A1")
->getNumberFormat()
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_PERCENTAGE_00);
Note: setFormatCode() seems to not support to apply to a entire column/row. So, if it is your case, you will need to create a loop over column/row to apply for each cell individually.

phpexcel thousand separator for xlsx

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

Categories