Store large number in a cell with PHPExcel - php

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

Related

phpspreadsheet not retrieving values for certain cell

Quite a weird one. I have a spreadsheet that I am trying to read via Phpspreadsheets, but one cell (at least for the moment), is not retrieving any data and I can't figure out why.
The text in B10 (not retrieving) is Mike
The text in B9 (retrieving) is Name
1) I have checked that the formatting on this cell (B10) and cells where data is retrieved is the same
2) I have tried getvalue, getformattedvalue, getcalculated value, all to no avail
Below is my Php code snippet:
$A1 = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 9)-
>getCalculatedValue(); // Returns value
echo $A1;
$A2 = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 10)-
>getValue(); //returns nothing
echo $A2;
There are many other cells with the same problem, but if can get this one, I'm sure that it's the same for the others...
Where can I start looking? What am I missing (something obvious I'm sure)...
I think that I may have found the answer...
I was loading an xlsm file, and not a plain xls or xlsx file. As soon as I changed to either of these formats, it seemed to behave correctly...

PHPExcel duplicate style only duplicate the first cell even when range of cell is given

I'm trying to copy styles from a range of cells in a template file and paste it to another range of cells in another file and then save the resulting file. I experimented on a small subset of my Excel file before I fully implemented it, but I found a strange result.
Here is my code snippet...
include "../class/PHPExcel.php";
include "../class/PHPExcel/IOFactory.php";
include "../class/PHPExcel/Cell.php";
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$frTemplate = $reader->load("document/Template/FR-001 Template.xlsx");
$frTemplate->setActiveSheetIndex(0);
$frTemplate->getActiveSheet()->setCellValueExplicitByColumnAndRow(3, 6, $year);
//copy style
$cellStyle = $frTemplate->getActiveSheet()->getStyle('E47:E49');
$frTemplate->getActiveSheet()->duplicateStyle($cellStyle, 'E52:E54');
//copy value
$cellValues = $frTemplate->getActiveSheet()->rangeToArray('E47:E49');
$frTemplate->getActiveSheet()->fromArray($cellValues, null,'E52');
$writer = PHPExcel_IOFactory::createWriter($frTemplate, "Excel2007");
$writer->save("document/Template/FR-001 2017.xlsx");
echo "Done";
Here is an image of the copied Excel file...
Copied File-Case 1
And here is an image of the target file...
Result-Case 1
I'm trying to apply the code with another data, and I found that -
"Merge cell" style cannot be copied
When a range of cells is copied, the only style copied is the first cell (upper left)
Sadly, I cannot post the evidence of the above case as my reputation is still very low (It is my first question in Stack Overflow!)
So, can you please tell me what is wrong with my code? Thanks in advance.
That's correct:
"Merge cell" is not an aspect of style; it's part of the structure of the worksheet, so it cannot be "copied" by duplicateStyle. If you want to merge cells, you need to use the mergeCells() method for each merge range that you want to set.
The duplicateStyle() method copies a style to a range of cells; it doesn't copy multiple styles to a new range. The getStyle() method allows a range for the purpose of setting a style against all cells in that range, but only returns the style of the first cell within that range. You need to duplicate each different style to its own range.

Set auto height in PHPExcel not working

I am generating Excel using PHPExcel.
All Code Works Fine.But Auto height code is not Working.
I have tried following code.
Apply row height on specific row
$objPHPExcel->getActiveSheet()->getRowDimension('7')->setRowHeight(-1);
Apply row height on for all row
$objPHPExcel->getActiveSheet()->getDefaultRowDimension(1)->setRowHeight(-1);
I have also tried word wrap property with it.
$objPHPExcel->getActiveSheet()
->getStyle('B7')
->getAlignment()
->setWrapText(true);
But it give me result as below:
Note : Working in MS office,Not Working in Apache open Office and
LibreOffice
Have just added the following to the 01simple.php example
$value = "To be or not to be-that is the question: whether 'tis nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and, by opposing, end them.";
$objPHPExcel->getActiveSheet()->setCellValue('A12', $value);
$objPHPExcel->getActiveSheet()->getRowDimension(12)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getStyle('A12')->getAlignment()->setWrapText(true);
and this creates correctly wrapped output for both Excel2007 and Excel5 Writers

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 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 !

Categories