Formatting a cell to a percentage in PHPExcel

天大地大妈咪最大 提交于 2019-12-04 22:44:34

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
    ]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!