joseagaleanoc
joseagaleanoc

Reputation: 615

How can I do a line break in cell of excel using PHP?

Hi I have thw following code:

header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=archivo.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo "
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="expires" content="0">
</head>
<body>
<table width="100%" border="1" align="center" cellpadding="3" cellspacing="0">
  <tr>
    <td>Line 1<br>Line 2</td>
  </tr> 
</table>
</body>
</html>";

I have tried:

<td>Line 1\r\nLine 2</td>
<td>Line 1".chr(10) . chr(13)."Line 2</td>

But nothing seems to work, I need to do a line break in a cell, any suggestions are welcomed. I'm using excel 2010.

I already look at this question: line break within data for Excel 2003 and it didnt work.

It doesnt show error, when I apply <br> it creates a new row, when I use \r\n, \n, or chr(10) . chr(13), it creates a space.

Thanks!!

Upvotes: 4

Views: 26513

Answers (4)

BobGao
BobGao

Reputation: 790

You should add double quotes to include line break

'<td>Line 1' . "\r\n" . 'Line 2</td>'

Upvotes: 1

joseagaleanoc
joseagaleanoc

Reputation: 615

After searching a lot found this site: http://www.bennadel.com/blog/1095-maintaining-line-breaks-in-an-html-excel-file.htm

And this tag solves the issue <br style="mso-data-placement:same-cell;" />

Thanks for all your comments!!

Upvotes: 13

robin
robin

Reputation: 159

You say charset="utf-8" but chr returns ASCII!

What about this? ;oP

utf8_encode(chr(10).chr(13))

Upvotes: 0

l0gic
l0gic

Reputation: 131

Have a shot with PHP_EOL it should give you the newline character of the operating system you're working on.

Example:

$output = 'Line 1' . PHP_EOL .
          'Line 2' . PHP_EOL .
          'Line 3';

Upvotes: 0

Related Questions