24 Votes

PHPExcel: Create Multi Line Cell

Question by PC Control | Last update on 2021-06-28 | Created on 2015-02-07

I would like to insert a line break within one table cell using PHPExcel. In other words, I would like to create a field containing multiple lines.

I have just tried to write some content with the usual subjects into the cell (that is inserting a string with \n or \r\n), but all of this is not leading to any success.

Is it at all possible to write multiline values to cells using PHPExcel? Or what am I doing wrong here?

ReplyPositiveNegativeDateVotes
8Best Answer10 Votes

Your approach using the \n came out okay, but you have also care about setting the WrapText property of the desired cell to true to make Excel interpreting the line break correctly.

$s = "Line1\nLine2"

$objPHPExcel->getActiveSheet()->setCellValue('A1', $s);
$objPHPExcel->getActiveSheet()->getStyle('A1')
                              ->getAlignment()
                              ->setWrapText(true);

Additionally, it is important to always specify the value using double quotes, otherwise the interpretation of \n is also not working.
Last update on 2021-06-28 | Created on 2015-02-09

ReplyPositive Negative
33 Votes

setWrapText is not working in office 2013 but it works in 2007 correctly.
2018-05-26 at 10:12

Positive Negative
Reply
Reply

Related Topics

HTML: Table - Merge Cells

Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.