about possibility

Topics: Developer Forum
Dec 15, 2011 at 9:27 AM

is that possible to apply two colours for two words in single cell through phpexcel code.

Coordinator
Dec 15, 2011 at 10:55 AM

Yes it is possible, using rich text. This is descibed in the developer documentation (section 4.6.37) and there is an example in 05featuredemo.inc.php

Dec 16, 2011 at 4:37 AM

can you pls send me sample php excel code to apply two colours for two words in single cell.

Coordinator
Dec 16, 2011 at 6:35 AM
$twoWords = new PHPExcel_RichText();

$textColor1 = $twoWords->createTextRun('TWO ');
$textColor1->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_RED ) );
$textColor2 = $twoWords->createTextRun('WORDS');
$textColor2->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_BLUE ) );

$objPHPExcel->getActiveSheet()->getCell('A1')->setValue($twoWords);
Dec 19, 2011 at 7:04 AM

i used rich text to do that, but colours are not applying.

this is my code

 

<?php

 

require_once 'Classes/PHPExcel.php';

require_once 'Classes/PHPExcel/IOFactory.php';



// Create new PHPExcel object

$objPHPExcel = new PHPExcel();



// Create a first sheet, representing sales data

$objPHPExcel->setActiveSheetIndex(0);



// Rename sheet

$objPHPExcel->getActiveSheet()->setTitle('Active-Inactive question sheet');



 

// set default font

$objPHPExcel->getDefaultStyle()->getFont()->setName('Tahoma');

$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);

$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true)->getStyle('A:Z')->getAlignment()->setHorizontal('center');

$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true)->getStyle('A1:Z1')->getAlignment()->setHorizontal('center');



$twoWords = new PHPExcel_RichText();



$textColor1 = $twoWords->createTextRun('TWO');

$textColor1->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_RED ) );

$textColor2 = $twoWords->createTextRun('WORDS');

$textColor2->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_BLUE ) );



$objPHPExcel->getActiveSheet()->getCell('A1')->setValue($twoWords);







$twoWords = new PHPExcel_RichText();



$textColor1 = $twoWords->createTextRun('TWO');

$textColor1->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_RED ) );

$textColor2 = $twoWords->createTextRun('WORDS');

$textColor2->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_BLUE ) );



$objPHPExcel->getActiveSheet()->getCell('A1')->setValue($twoWords);









  $Month = date('M');

  $Day = date('d').date('S');

  $Year = date('y');





$file_format ="$Day $Month'$Year ";

 

  

header('Content-Type: application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="Richtext on '.$file_format.'.xls"');

header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

?>

Coordinator
Dec 19, 2011 at 8:58 AM

Rich text currently only applies in the Excel2007 writer

Dec 19, 2011 at 9:09 AM

i am using excel 2007 only.

Dec 19, 2011 at 9:14 AM

you're using the Excel5 writer, not excel2007

Dec 19, 2011 at 9:49 AM

sorry, yes you are correct i am using 2005 only i am new to php excel i am not getting how to do.