65

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 :)

pnuts
  • 58,317
  • 11
  • 87
  • 139
bungdito
  • 3,556
  • 4
  • 31
  • 38
  • 2
    Does your actual code reference cell H5 or H% – Mark Baker May 11 '11 at 06:43
  • @Mark Baker, sorry i just mistype. actually this is for cell H5. i just update my question, many thanks :) – bungdito May 11 '11 at 06:47
  • 2
    OK, second question (seeing as you changed it when you edited): 'Hello\nWorld' or "Hello\nWorld"? Standard PHP quoting for strings applies: the double quotes make a difference to the \n – Mark Baker May 11 '11 at 08:07

5 Answers5

132
$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.

wimvds
  • 12,790
  • 2
  • 41
  • 42
  • 6
    It is useful to note that if you are doing this to a cell you have created by merging other cells you should specify all of the cells by their original mapping. Eg if you create B2 by merging B2-E6 then the argument to getStyle is 'B2:E6' – jerrygarciuh Nov 23 '13 at 03:45
  • this is working in your example but i want output like Hello \n World DEMO(new line after Hello).but here two new line created so how i solve this? – himansu Jul 31 '15 at 07:18
  • yes, content must be wrapped with double-quotes. Thank you. – Bhavin Thummar Dec 05 '19 at 07:30
32

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);
Agriesean
  • 155
  • 1
  • 16
Ravin
  • 371
  • 1
  • 3
  • 5
11

Improved answer based on Ravin and others

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

$objPHPExcel
  ->getActiveSheet()
  ->getStyle('H5')
  ->getAlignment()
  ->setWrapText(true);
fehrlich
  • 2,497
  • 2
  • 26
  • 44
Muhammad Amjad
  • 306
  • 3
  • 9
  • 5
    `PHP_EOL` is useless in this case, because it will give an EOL char based on the OS where the php script is run (so likely linux/unix server), but the file will likely be opened on Windows, where default EOL will be different. – mkilmanas Mar 02 '18 at 14:08
  • @Muhammad what about if I have data from MySQL? Like how will I put a new line same for $row['abc']. Can you help Thanks – Mazhar Iqbal Rana Apr 04 '23 at 03:19
10

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

Andron
  • 6,413
  • 4
  • 43
  • 56
-7

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>
Pang
  • 9,564
  • 146
  • 81
  • 122