3

I am trying to create a table with cells width a fixed width.
Let's say I have this code:

$table->addCell(300)->addText('first');
$table->addCell(300)->addText('text that is very looooooooong');

The first cell's 300 width is ignored and is crushed against the left side of the page like this:

f
i
r
s
t

My second cell is going to contain large texts, but I want that cell to maintain it's width.
I couldn't find it anywhere on the web so I am asking if somebody knows what I will have to do here.

4

2 回答 2

4

请记住,这些值为 TWIPS,而 300 Twips 仅为 5 毫米。没有足够的空间容纳超过 1 个字符。

这是一个可以帮助您转换值的计算器:

Topgrapy 计算器

您也可以像这样使用 Shared\Font.php 中的内置转换函数:

$helper= new PHPWord_Shared_Font();
//convert 5cm to Twips
$inTwips=$helper->centimeterSizeToTwips(5);

到目前为止,我还没有尝试过。

于 2013-11-26T14:14:37.023 回答
1

我刚刚发现你必须在你的桌子上设置样式“布局”,如下所示

$fancyTableStyle = [
    'borderSize'  => 6,
    'borderColor' => '000000',
    'cellMargin'  => 80,
    'alignment'   => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,
    'layout'      => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED,
];
$table = $section->addTable($fancyTableStyle);

然后长词将自己包裹在单元格内。

如果您在 phpword 对象上预设了表格样式,则布局样式将不起作用。

$fancyTableStyleName = 'Fancy Table';
$fancyTableStyle = [
    'borderSize'  => 6,
    'borderColor' => '000000',
    'cellMargin'  => 80,
    'alignment'   => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,
    'layout'      => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED,
];
//table will not be fixed
$table = $section->addTable($fancyTableStyleName);

在 ms word 中,您还需要直接在每个表上设置自动调整选项,也许这就是原因。

于 2019-03-27T06:49:35.667 回答