Reputation: 1764
I'm using PHPExcel to export into an excel file and new lines worked great until I needed to use PHPExcel_RichText()
to get different formatting in the same cell. Is there any way around this? (I know that PHPspreadsheet is available but I have to use PHPExcel). I found a few threads about similar issues but no solutions unfortunately.
$objRichText = new PHPExcel_RichText();
$run1 = $objRichText->createTextRun("Line one\n this does not work");
$run1->getFont()->applyFromArray(array( "bold" => true, "size" => 24, "name" => "Calibri", "color" => array("rgb" => "333333")));
$objRichText->createText("\n this does not work either \r\n and not this");
$run2 = $objRichText->createTextRun("Line two");
$run2->getFont()->applyFromArray(array( "bold" => true, "size" => 16, "name" => "Calibri", "color" => array("rgb" => "333333")));
$objWorksheet->setCellValueByColumnAndRow(0, 1, $objRichText);
Upvotes: 2
Views: 1199
Reputation: 115
What worked for me was to ensure the cell has WrapText set...
$objPHPExcel->getActiveSheet()->getStyle("A1")->getAlignment()->setWrapText(true);
Upvotes: 1
Reputation: 83
This might be work for you.
$objRichText = new PHPExcel_RichText();
$objRichText->createText('XYZ ');
$objPayable = $objRichText->createTextRun('ABC');
$objRichText->createTextRun("\r\n");
$objPayable->getFont()->setBold(true);
$objPayable->getFont()->setItalic(true);
$objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
$objRichText->createText(', unless specified otherwise on the invoice.');
$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Rich Text')
->setCellValue('C1', $objRichText);
$objRichText2 = new PHPExcel_RichText();
$objRichText2->createText("black text\n");
$objRed = $objRichText2->createTextRun("red text");
$objRed->getFont()->setColor( new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED ) );
$objPHPExcel->getActiveSheet()->getCell("C2")->setValue($objRichText2);
$objPHPExcel->getActiveSheet()->getStyle("C2")->getAlignment()->setWrapText(true);
Upvotes: 0
Reputation: 2387
Trying the PHP_EOL
instead of '\n' solved the issue for me
$objRichText->createText(PHP_EOL);
DOS - Uses a CR+LF (that's ASCII 13 followed by an ASCII 10, or \r\n
) to represent a new line.
Unix - Uses an LF (that's ASCII 10, or \n
) to represent a new line.
Mac (pre-OS X) - Uses a CR (that's ASCII 13, or \r
) to represent a new line.
Mac (OS X) - Like Unix, uses an LF to represent a new line.
PHP_EOL
will automatically choose the correct character for the platform, so that your new lines are platform-independent.
Upvotes: 1