Home » Php » php – How to make the table in word using PHPWord which includes multiple rowspan and colspan?

php – How to make the table in word using PHPWord which includes multiple rowspan and colspan?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have been learning the PHPWord for my academic project, and I also have the latest PHPWord library which supports "vMerge" for rowspan and "gridSpan" for colspan.

I have been finding difficulty in creating one particular type of table structure as shown in the Image below.

enter image description here

The problem is how to have same rowspan for ‘1’,’2′ and ‘6’ , in this case it is equal to 2.

Any kind of help would be appreciated.

edit-1 I have succeeded with basic rowspan and colspan, but I am finding difficulty with this complex table.

How to&Answers:

There is an example in the samples that is quite close to what you are doing: https://github.com/PHPOffice/PHPWord/blob/develop/samples/Sample_09_Tables.php

and modifying that a bit to achieve your example:

$cellRowSpan = array('vMerge' => 'restart');
$cellRowContinue = array('vMerge' => 'continue');
$cellColSpan = array('gridSpan' => 2);

$table->addRow();
$table->addCell(2000, $cellRowSpan)->addText("1");
$table->addCell(2000, $cellRowSpan)->addText("2");
$table->addCell(4000, $cellColSpan)->addText("3");
$table->addCell(2000, $cellRowSpan)->addText("6");

$table->addRow();
$table->addCell(null, $cellRowContinue);
$table->addCell(null, $cellRowContinue);
$table->addCell(2000)->addText("4");
$table->addCell(2000)->addText("5");
$table->addCell(null, $cellRowContinue);

$table->addRow();
$table->addCell(2000);
$table->addCell(2000);
$table->addCell(2000);
$table->addCell(2000);
$table->addCell(2000);

tested with 0.13.0 version (for some reason libreoffice didn’t like the two adjecent cells with vMerge continue definitions and didn’t display them as expected, but word did display them nicely as expected)