PHPExcel - 如何简化设置含有2种字体大小的单元格

3

我正在使用PHPExcel创建电子表格...我想要在同一个单元格中创建14pt字体和10pt字体的文本。

我已经能够创建一个测试程序,但是它似乎比我想象中的要冗长得多。

有人可以向我展示如何简化设置这两个文本段的字体吗?

以下是我为测试创建的代码...

<?php
require_once './Classes/PHPExcel.php';
require_once './Classes/PHPExcel/IOFactory.php';

$objPHPExcel = new PHPExcel();

$phpColor = new PHPExcel_Style_Color();
$phpColor->setRGB("0070C0");

$objRichText = new PHPExcel_RichText();
$run1 = $objRichText->createTextRun('Main Part of the Title, ');
$run1->getFont()->setBold(true);
$run1->getFont()->setName("Calibri");
$run1->getFont()->setSize("14");
$run1->getFont()->setColor($phpColor);

$run2 = $objRichText->createTextRun(' and some extra');
$run2->getFont()->setBold(true);
$run2->getFont()->setName("Calibri");
$run2->getFont()->setSize("10");
$run2->getFont()->setColor($phpColor);

$objPHPExcel->getActiveSheet()->setCellValue("A1", $objRichText);
header ( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
header ( 'Content-Disposition: attachment;filename="sq_test.xlsx"' );
header ( 'Cache-Control: max-age=0' );
$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel2007' );
$objWriter->save ( 'php://output' );
?>

具体而言,我尝试使用以下内容代替所有的 $run2->getFont() 代码行...

$run2->getFont()->applyFromArray(array("font" => array( "bold" => true, "size" => 10, "name" => "Calibri", "color" => $phpColor)));

它并没有生成错误,但也没有调整字体到其默认设置之外……但如果可能的话,这种简化是我非常希望的。

2个回答

3

您可以使用流畅接口稍微简化:

$objRichText = new PHPExcel_RichText();
$run1 = $objRichText->createTextRun('Main Part of the Title, ');
$run1->getFont()->setBold(true);
$run1->getFont()->setName("Calibri");
$run1->getFont()->setSize("14");
$run1->getFont()->setColor($phpColor);

可以变成

$objRichText = new PHPExcel_RichText();
$run1 = $objRichText->createTextRun('Main Part of the Title, ')
    ->getFont()->setBold(true)
        ->setName("Calibri")
        ->setSize("14")
        ->setColor($phpColor);

你尝试从数组中应用样式可能失败,因为你的数组包含了对顶层font的引用,并且你正在尝试将其设置为字体... 尝试仅为可应用于字体的属性设置它:

$run2->getFont()
    ->applyFromArray(array( "bold" => true, "size" => 10, "name" => "Calibri", "color" => $phpColor)
);

我个人没有直接尝试过这个,所以不能保证其可行性。


这让我完成了90%的工作。除了颜色(因为写法不正确而无法使用...传递的样式数组无效)...我会看看能否弄清楚如何解决这个问题。感谢你的回复,马克。 - NWT_Bob
1
我的颜色错误是将颜色作为 PHPExcel_Style_Color 传递。当我把它改成 color => array("rgb" => "0070C0") 就可以了。谢谢。 - NWT_Bob

3

最终版本,每个文本部分只有两行(非常感谢@Mark_Baker),如下:

<?php
require_once './Classes/PHPExcel.php';
require_once './Classes/PHPExcel/IOFactory.php';

$objPHPExcel = new PHPExcel();

$phpColor = new PHPExcel_Style_Color();
$phpColor->setRGB("0070C0");

$objRichText = new PHPExcel_RichText();
$run1 = $objRichText->createTextRun('Main Part of the Title, ');
$run1->getFont()->applyFromArray(array( "bold" => true, "size" => 14, "name" => "Calibri", "color" => array("rgb" => "0070C0")));

$run2 = $objRichText->createTextRun(' and some extra');
$run2->getFont()->applyFromArray(array( "bold" => true, "size" => 10, "name" => "Calibri", "color" => array("rgb" => "0070C0")));

$objPHPExcel->getActiveSheet()->setCellValue("A1", $objRichText);
header ( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
header ( 'Content-Disposition: attachment;filename="sq_test.xlsx"' );
header ( 'Cache-Control: max-age=0' );
$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel2007' );
$objWriter->save ( 'php://output' );
?>

希望这能帮助其他人顺利进行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接