Quantcast
Channel: PHPExcel Forum Rss Feed
Viewing all articles
Browse latest Browse all 2707

New Post: New line in rich text cell prior to variable not working

$
0
0
Seems to work
$outputFileType = 'Excel2007';
$outputFileName = './outputTest.xlsx';

$objPHPExcel = new PHPExcel();
$worksheet = $objPHPExcel->getActiveSheet();

// Plaintext example
$worksheet->setCellValue('A1',"Hello\nWorld");
$worksheet->getRowDimension(1)->setRowHeight(-1);
$worksheet->getStyle('A1')->getAlignment()->setWrapText(true);

// Rich text example #1
$objRichText1 = new PHPExcel_RichText();
$objGreen1 = $objRichText1->createTextRun("Hello");
$objGreen1->getFont()->setColor(
    new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_DARKGREEN)
);
$objRed1 = $objRichText1->createTextRun("\nWorld");
$objRed1->getFont()->setColor(
    new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_DARKRED)
);

$worksheet->getCell('A2')->setValue($objRichText1);
$worksheet->getRowDimension(1)->setRowHeight(-1);
$worksheet->getStyle('A2')->getAlignment()->setWrapText(true);

// Rich text example #2
$words = ['Hello', 'World'];
$objRichText2 = new PHPExcel_RichText();
$objGreen2 = $objRichText2->createTextRun("\n" . $words[0]);
$objGreen2->getFont()->setColor(
    new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_DARKGREEN) 
);
$objRed2 = $objRichText2->createTextRun("\n" . $words[1] . "\n");
$objRed2->getFont()->setColor(
    new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_DARKRED) 
);

$worksheet->getCell('A3')->setValue($objRichText2);
$worksheet->getRowDimension(1)->setRowHeight(-1);
$worksheet->getStyle('A3')->getAlignment()->setWrapText(true);


$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType);
$objPHPExcel = $objPHPExcelWriter->save($outputFileName);
I suspect that the problem is that you're not setting the cell to allow text wrapping:
$objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setWrapText(true);
and you may also want to set the column width to autosize as well
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);

Viewing all articles
Browse latest Browse all 2707

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>