我正在尝试创建一个单元格宽度为固定宽度的表。
假设我的代码如下:
$table->addCell(300)->addText('first');
$table->addCell(300)->addText('text that is very looooooooong');
第一个单元格的300宽度被忽略,并被压在页面的左侧,如下所示:
f
i
r
s
t
我的第二个单元格将包含大文本,但我希望那个单元格保持它的宽度。
但我在网上怎么也找不到解决方法,所以是否有人知道这里要做什么?
Remeber that these values are TWIPS and 300 Twips are mere 5mm. Not enough room to hold more than 1 character.
Here is a calculator that helps you to convert values:
Also you might use the built in conversion functions in Shared\Font.php like this:
$helper= new PHPWord_Shared_Font();
//convert 5cm to Twips
$inTwips=$helper->centimeterSizeToTwips(5);
I have not tried this so far.
I just found out that you have to set style "layout" fixed on your table like below
$fancyTableStyle = [
'borderSize' => 6,
'borderColor' => '000000',
'cellMargin' => 80,
'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,
'layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED,
];
$table = $section->addTable($fancyTableStyle);
and then the long word will wrap itself inside the cell.
If you preset a table style on your phpword object, the layout style won't work.
$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);
In ms word you also need to set the autofit option directly on each table, maybe that's why.