I have a spreadsheet I'm creating with PHPExcel ... and I'm trying to create a single cell that has 14pt text followed by 10pt font (all within the same cell).
I've been able to create a test routine that works ... but it seems a lot more verbose that I think it should be.
Can someone show me how I can simplify setting the font for each of these two segments of text.
Here is the code I created for testing ...
<?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' );
?>
Specifically I tried using the following instead of all the $run2->getFont() lines ...
$run2->getFont()->applyFromArray(array("font" => array( "bold" => true, "size" => 10, "name" => "Calibri", "color" => $phpColor)));
And it didn't generate an error ... but didn't adjust the font from it's default settings ... but this is the kind of simplification that I would really like if it's possible.
So the final version, with only 2 lines for each portion of the text, (much thanks to @Mark_Baker) is:
<?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' );
?>
Hope this helps someone else along the way. ,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With