Formatting a cell to a percentage in PHPExcel
Asked Answered
A

4

23

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.

Astrea answered 7/5, 2013 at 21:21 Comment(1)
you can use ceil() to round to closest number and than append % before putting it in ur excel file..php.net/manual/en/function.ceil.phpBurthen
F
46

assuming your cell is A1 ..

$objPHPExcel->getActiveSheet()->getStyle('A1')
    ->getNumberFormat()->applyFromArray( 
        array( 
            'code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00
        )
    );
Fully answered 7/5, 2013 at 21:43 Comment(4)
I guess that to get a straight percentage i.e. 2% i need to remove the _00 form FORMAT_PERCENTAGE_00 ?Astrea
Use PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE to display as an integer percentage; of "0.0%" or "0.000%" etc to display one or three decimal places.... or indeed any string that corresponds to a valid Excel number format maskTreasonable
taking off _00 is the key!!! I tried it and it works like charm! Thank you @ChrisHairspring
good to see my questions being used :D good luck! thanks for the appreciation!Astrea
Z
3

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%');
Zarah answered 28/8, 2016 at 13:53 Comment(0)
A
0

You can try this code:

$colLetter = "A";
$rowNumber = "1";

$objPHPExcel->getActiveSheet()
    ->getStyle("$colLetter:$rowNumber")
    ->getNumberFormat()
    ->applyFromArray([
        "code" => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE
    ]);
Approbation answered 19/7, 2017 at 10:20 Comment(2)
You should add an explanation of what your answer adds to the other.Messuage
Yes of course you have right @gp_sflover, but I thought this piece of code was self-describing :)Approbation
L
0

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.

Lafferty answered 9/7, 2021 at 13:37 Comment(1)
setFormatCode is a function in PHPExcel. The applyFromArray is used to set multiple styles as once. Also, you can uase "A1:E10" for the cell ID to set a format to multiple cells.Laclair

© 2022 - 2024 — McMap. All rights reserved.