在PHPExcel中设置字体颜色,字体和字体大小 [英] Set Font Color, Font Face and Font Size in PHPExcel

查看:12578
本文介绍了在PHPExcel中设置字体颜色,字体和字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHPExcel工作。我是初学者。当我使用以下代码并且工作正常。

I'm working in PHPExcel. I'm beginner.When I'm using following code and its working fine.

$phpExcel = new PHPExcel();

$phpExcel->getActiveSheet()->getStyle("A1")->getFont()->setBold(true)
                                ->setName('Verdana')
                                ->setSize(10)
                                ->getColor()->setRGB('6F6F6F');

但是当我使用下面的代码并没有得到如上所述的预期结果。

But when I'm using following code and not getting expected result as above.

$phpFont = new PHPExcel_Style_Font();
$phpFont->setBold(true);
$phpFont->setName('Verdana');
$phpFont->setSize(15);

$phpColor = new PHPExcel_Style_Color();
$phpColor->setRGB('FF0000');  

$phpExcel->getActiveSheet()->getStyle('A1')->setFont( $phpFont );
$phpExcel->getActiveSheet()->getStyle('A1')->getFont()->setColor( $phpColor );

请帮助我在上面的代码中做错了。

Please help me what am I doing wrong in above code.

提前感谢!

推荐答案

我建议您开始阅读文档(4.6.18。格式化单元格)。当应用很多格式化时,最好使用 applyFromArray()根据文档,此方法也假设在设置许多样式属性时更快。有一个附件,您可以在其中找到此功能的所有可能的键。

I recommend you start reading the documentation (4.6.18. Formatting cells). When applying a lot of formatting it's better to use applyFromArray() According to the documentation this method is also suppose to be faster when you're setting many style properties. There's an annex where you can find all the possible keys for this function.

这将为您工作:

$phpExcel = new PHPExcel();

$styleArray = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => 'FF0000'),
        'size'  => 15,
        'name'  => 'Verdana'
    ));

$phpExcel->getActiveSheet()->getCell('A1')->setValue('Some text');
$phpExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);

这篇关于在PHPExcel中设置字体颜色,字体和字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆