php PHPExcel中设置字体颜色、字体字体和字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17317301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Font Color, Font Face and Font Size in PHPExcel
提问by som
I'm working in PHPExcel. I'm beginner.When I'm using following code and its working fine.
我在 PHPExcel 中工作。我是初学者。当我使用以下代码并且工作正常时。
$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.
请帮助我在上面的代码中我做错了什么。
Thank you in advance!
先感谢您!
回答by Max
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.
我建议您开始阅读文档(4.6.18。格式化单元格)。当应用大量格式时,最好使用applyFromArray()
根据文档,当您设置许多样式属性时,这种方法也应该更快。有一个附件,您可以在其中找到此功能的所有可能键。
This will work for you:
这对你有用:
$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);
To apply font style to complete excel document:
要应用字体样式以完成 Excel 文档:
$styleArray = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => 'FF0000'),
'size' => 15,
'name' => 'Verdana'
));
$phpExcel->getDefaultStyle()
->applyFromArray($styleArray);