how to make New lines in a cell using phpexcel

PhpPhpexcel

Php Problem Overview


i have problem with php excel,

i want to make new line in one cell but i can't, i have tried using \n or <br /> but itsn't work. this my code:

$objPHPExcel->getActiveSheet()->setCellValue('H5', 'Hello\nWorld'); // i need this show in two line
$objPHPExcel->getActiveSheet()->getStyle('H5')->getAlignment()->setWrapText(true);

fyi: my format excel is xls not xlsx. many thanks :)

Php Solutions


Solution 1 - Php

$objPHPExcel->getActiveSheet()->setCellValue('H5', "Hello\nWorld");
$objPHPExcel->getActiveSheet()->getStyle('H5')->getAlignment()->setWrapText(true);

Works for me...

You should always use double quotes when you add escape sequences in a PHP string.

Solution 2 - Php

you should use 'r' to break into new line into excel with php

and use double quotes when you add escape sequences in a PHP string.

  $objPHPExcel->getActiveSheet()->setCellValue('H5', "Hello\r World");
  $objPHPExcel->getActiveSheet()->getStyle('H5')->getAlignment()->setWrapText(true);
  

Solution 3 - Php

Improved answer based on Ravin and others

$objPHPExcel
  ->getActiveSheet()
  ->setCellValue('H5', "Hello".PHP_EOL." World");

$objPHPExcel
  ->getActiveSheet()
  ->getStyle('H5')
  ->getAlignment()
  ->setWrapText(true);

Solution 4 - Php

We can set for the default style, so don't need to specify for each cell range:
$objPHPExcel->getDefaultStyle()->getAlignment()->setWrapText(true);

Solution 5 - Php

To achieve next line but same cell forxcel export, this is the simplest solution.

<tr>
    <td style="wrap-text: true">
        Test
        <br />
        Test2
    </td>
</tr>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbungditoView Question on Stackoverflow
Solution 1 - PhpwimvdsView Answer on Stackoverflow
Solution 2 - PhpRavinView Answer on Stackoverflow
Solution 3 - PhpMuhammad AmjadView Answer on Stackoverflow
Solution 4 - PhpAndronView Answer on Stackoverflow
Solution 5 - PhpAmbrish DharaneView Answer on Stackoverflow