PHPWord如何wordwrap文本?

时间:2016-08-24 08:56:25

标签: php phpword

我目前在Word2007,Word2013中测试了一个问题,它在LibreOffice中运行良好。

我的文本来自单元格内的mysql数据库,它不尊重换行符。

Word2007中的示例:
text = - car - plane - sofa - office

LibreOffice中的示例(Ubuntu 16 LTS):
- 车
- 飞机
- 沙发
- 办公室

我在PHPWord 'wordWrap'=>true中找到了一个选项,但似乎没有任何效果。

让某人知道这个问题并且可以给我一个有效的例子吗?

编辑:在包含单元格的表中尝试第一个解决方案。 (以下代码位于while循环内,因此如果有10个,则重新创建10倍的单元格

$table->addRow($height);
$table->addCell($width, $styleVertical)->addText("$risques_identifies", $styleTextNextRow, $styleLeft);
$table->addCell($width, $styleVertical)->addText("$epi_epc", $styleTextNextRow, $styleLeft);

$table->addCell($width, $styleVertical);
$textlines_organisation_documentation=explode("\n",$organisation_documentation);
for ($i = 0; $i < sizeof($textlines_organisation_documentation); $i++) {
$table->addText("$textlines_organisation_documentation", $styleTextNextRow, $styleLeft);
}

我的代码会产生错误。我不知道如何创建一个单元格并在之后添加文本(在“for”中)。这就是我想要做的。

1 个答案:

答案 0 :(得分:2)

这将为\n

定义的每个段落插入换行符

编辑(感谢@CBroe)

$text = "foo\nbar\nfoobar";
$textlines = explode("\n", $text);

// Create table...
// Add table cell
$mycell = $table->addCell()

// Loop content to append cell
for ($i = 0; $i < sizeof($textlines); $i++) {
    $mycell->addText($textlines[$i]);
}
相关问题