我怎么能做一个换行在excel单元格使用PHP


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

你好,我有以下代码:

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

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

但是似乎没有工作,我需要在单元格中做一个换行,欢迎任何建议。我用excel 2010。

我已经看了这个问题:在Excel 2003的数据中换行,它不起作用。

它没有显示错误,当我应用<br>时,它创建一个新行,当我使用'r'n, 'n, or chr(10) . chr(13)时,它创建一个空格。

谢谢! !

经过一番搜索发现了这个网站:http://www.bennadel.com/blog/1095-maintaining-line-breaks-in-an-html-excel-file.htm

这个标签解决了<br style="mso-data-placement:same-cell;" />

这个问题

谢谢你的评论!!

你应该添加双引号来包括换行

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

你说charset="utf-8"但是chr返回的是ASCII!

这个怎么样?, oP

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

试试PHP_EOL,它应该会给你正在使用的操作系统的换行符。

的例子:

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