4

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!!

Community
  • 1
  • 1
joseagaleanoc
  • 575
  • 2
  • 7
  • 20

4 Answers4

11

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!!

joseagaleanoc
  • 575
  • 2
  • 7
  • 20
  • I was trying to export a wp list table to .csv. Couldn't for the life in me make the line breaks inside an cell work. I found your answer and tried to export to .xls instead: it's working like a charm! Thanks for the help. – Pablo Aug 29 '16 at 19:31
1

You should add double quotes to include line break

'<td>Line 1' . "\r\n" . 'Line 2</td>'
BobGao
  • 770
  • 8
  • 18
0

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

What about this? ;oP

utf8_encode(chr(10).chr(13))
robin
  • 159
  • 5
-1

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';
l0gic
  • 121
  • 5