I am using PHPExcel to write an entire array to a sheet and a couple of my columns need to have the value in every cell displayed as a percent (ie with the percent sign).
My problem is that when I try to format column G as a percentage with this code:
$format_percent = array('code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00);
$objWorksheet->getStyle("G2:G".$rc)->applyFromArray($format_percent);
The resulting file has all cells in column G still with the "General" number format...not formated as a percent as I had hoped.
NOTE: the $rc variable is storing the row count
Any ideas? Thanks in advance.
Think I figured it out. I was trying to apply the formatting to the whole column at once. That doesn't seem to work. I thought it would because I CAN apply formatting like Bold to the text or and entire row at once. However, when I did a test and applied the percent formatting to one cell, it worked fine. So now I have just decided to loop over all of my cells in my column and apply the formatting one at a time.
Related
I'm stuck with this for a few days about PHPExcel.
Why when it comes to 1000 the value convert to text automatically?
But It's normal if in the cell containing the number 0-999.
I have my code and the result below.
Thanks in advance.
Please try this
$objPHPExcel->getActiveSheet()->getStyle('H'.$row)->getNumberFormat()->setFormatCode('#,##0.00');
if you want General format rather than 'number', then set it to General Instead:
phpexcel -How to change data type for whole column of an excel
When I set format of a cell as %, it automatically multiplies the value with 100 & shows the value. So 94 becomes 9400%.
I want to apply % format on a column but want to keep the values same as before. I just want % sign to be shown along with the value. My Value should be 94% . To achieve this, I used following format code
$this->_xls_current_sheet->getStyle($cell_coordinates)->getNumberFormat()->applyFromArray(
array(
'code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE
));
If you store 0.5 in a cell, and format it as a percentage, then it will appear as 50%.... if you store 50 in a cell, and format it as a percentage, then it will appear as 5000% that is how it should be.
If you enter 50 as a number in the MS Excel GUI, and then set the format mask manually to a percentage, you will get a displayed value of 5000%. However, if you enter 50% in the MS Excel GUI, then MS Excel divides the value by 100 to store 0.5 in the cell, and formats it as a percentage (two actions in one step). If you set 50% in this way in MS Excel, and then change the format mask from percentage to number, then you will see that MS Excel has done this division for you.
PHPExcel doesn't do this (by default)... it expects you to set the correct value and the format as two separate steps. So PHPExcel expects you to store the correct value in the cell (0.5) in the first place, when you want to format it as a percentage.... it will not magically change the value in any way, it doesn't multiply it at all.
Note that if you are using the advanced value binder (rather than the default value binder), and set the cell value to 50%, then PHPExcel will divide by 100 and set to a percentage automatically, in the same way as the MS Excel GUI.
I was wondering if I can get the sum of an excel column, without getting the subformula for each item category(Tort and nach).
Take note that there are times there will be an additional item and will be added to the category; meaning I cannot specify the cell value (e.g, SUM(G12:G18) + SUM(G21:G26)).
Is there any way to do this using PHP Excel?
try: =SUM(G12:G18,G21:G26)
I have a diffent excel language, where I have to use a semi-colon in stead of a comma in the formula
I am using the below code to set the cell value, but it shows the output in the cell which is different than the original:
$excel2->getActiveSheet()->setCellValue("F2",'1234567890');
Output
$1,234,567,890.00
The actual output which I expect is 1234567890, but I don't know why it is showing like this, any help would be appreciated.
you should add number format to your cell
$excel2->getActiveSheet()->getStyle('F2') ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
For more information of cell styling have look here and look Style classes on left navigation links.
Use number format and set format code as per your requirement.
Try below code.
$excel2->getActiveSheet()->setCellValue("F2",'1234567890')->getNumberFormat()->SetFormatCode('#,##0');
I am reading excel files through php, the contents are to be displayed in a table on web
I read this data in range A1 to D1 is one data and it is to be presented in this format
The data
is different,
in every,
cell.
This data is to be <br/> line breaked based on strlen (This is a single string is different, in every, cell. Line one The data is heading)
So line breaks are made based on , but max data has to fit in single line
So how can I count strlen to each , so that I can echo if the data can fit in single line or else a line break is made after ,
The data
is different,
in every, cell.
Use strpos() with an offset in a loop in order to find the commas. After finding the first comma, use this position as the new offset for finding the next comma.
Have your tried the PEAR Excel Package(s)? It Simplifies things (reading/writing .xls files)
The str_getcsv() function added in PHP 5.3 may be of use to you.
If not, surely you could just explode(',', $string)?