php 如何使用phpexcel在单元格中创建新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5960242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:02:53  来源:igfitidea点击:

how to make New lines in a cell using phpexcel

phpphpexcel

提问by bungdito

i have problem with php excel,

我有 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:

我想在一个单元格中创建新行,但我不能,我尝试使用 \n 或 <br /> 但它不起作用。这是我的代码:

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

仅供参考:我的excel格式是xls而不是xlsx。非常感谢 :)

回答by wimvds

$objPHPExcel->getActiveSheet()->setCellValue('H5', "Hello\nWorld");
$objPHPExcel->getActiveSheet()->getStyle('H5')->getAlignment()->setWrapText(true);

Works for me...

对我有用...

You should always use double quoteswhen you add escape sequences in a PHP string.

在 PHP 字符串中添加转义序列时,应始终使用双引号

回答by Ravin

you should use 'r' to break into new line into excel with php

你应该使用 'r' 用 php 插入新行到 excel

and use double quotes when you add escape sequences in a PHP string.

并在 PHP 字符串中添加转义序列时使用双引号。

  $objPHPExcel->getActiveSheet()->setCellValue('H5', "Hello\r World");
  $objPHPExcel->getActiveSheet()->getStyle('H5')->getAlignment()->setWrapText(true);

回答by Muhammad Amjad

Improved answer based on Ravin and others

基于 Ravin 和其他人的改进答案

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

$objPHPExcel
  ->getActiveSheet()
  ->getStyle('H5')
  ->getAlignment()
  ->setWrapText(true);

回答by Andron

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

我们可以设置为默认样式,所以不需要为每个单元格范围指定:
$objPHPExcel->getDefaultStyle()->getAlignment()->setWrapText(true);

回答by Ambrish Dharane

To achieve next line but same cell forxcel export, this is the simplest solution.

要实现下一行但相同的单元格 forxcel 导出,这是最简单的解决方案。

<tr>
    <td style="wrap-text: true">
        Test
        <br />
        Test2
    </td>
</tr>