如何使用PHP在Excel的单元格中进行换行?

时间:2022-05-30 07:47:47

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.

但似乎没有任何效果,我需要在单元格中进行换行,欢迎任何建议。我正在使用excel 2010。

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

我已经看过这个问题:Excel 2003数据中的换行符并没有用。

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.

它没有显示错误,当我申请时它会创建一个新行,当我使用\ r \ n,\ n或chr(10)时。 chr(13),它创造了一个空间。

Thanks!!

谢谢!!

4 个解决方案

#1


7  

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

经过大量搜索后发现这个网站: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!!

谢谢你们的评论!!

#2


0  

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

有了PHP_EOL的镜头,它应该为您提供正在使用的操作系统的换行符。

Example:

例:

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

#3


0  

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

你说charset =“utf-8”但是chr返回ASCII!

What about this? ;oP

那这个呢? ;业务

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

#4


0  

You should add double quotes to include line break

您应该添加双引号以包含换行符

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

#1


7  

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

经过大量搜索后发现这个网站: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!!

谢谢你们的评论!!

#2


0  

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

有了PHP_EOL的镜头,它应该为您提供正在使用的操作系统的换行符。

Example:

例:

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

#3


0  

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

你说charset =“utf-8”但是chr返回ASCII!

What about this? ;oP

那这个呢? ;业务

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

#4


0  

You should add double quotes to include line break

您应该添加双引号以包含换行符

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