I'm using PHPWord to create a table like this:
My Code:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$fullWidth = 11906.85;
$cellRowSpan = ['vMerge' => 'restart'];
$cellRowContinue = ['vMerge' => 'continue'];
$cellColSpan2 = ['gridSpan' => 2];
$cellColSpan3 = ['gridSpan' => 3];
$cellColSpan4 = ['gridSpan' => 4];
$cellColSpan5 = ['gridSpan' => 5];
$cellColSpan6 = ['gridSpan' => 6];
$cell1Width = $fullWidth * 0.25;
$cell2Width = $fullWidth * 0.095;
$cell3Width = $fullWidth * 0.1725;
$cell4Width = $fullWidth * 0.0475;
$cell5Width = $fullWidth * 0.18;
$cell6Width = $fullWidth * 0.255;
$cell23Width = $cell2Width + $cell3Width;
$cell34Width = $cell3Width + $cell4Width;
$cell45Width = $cell4Width + $cell5Width;
$cell56Width = $cell5Width + $cell6Width;
$cell2to6Width = $fullWidth - $cell1Width;
$table = $section->addTable([
'borderColor' => 'cccccc',
'borderSize' => 6,
]);
$table->addRow();
$table->addCell($fullWidth, $cellColSpan6)->addText('Part A: Principal Investigator');
$table->addRow();
$table->addCell($cell1Width)->addText('Name:');
$table->addCell($cell2to6Width, $cellColSpan5);
$table->addRow();
$table->addCell($cell1Width)->addText('School/Dept at HKUMed or Faculty of Dentistry:');
$table->addCell($cell23Width, $cellColSpan2);
$table->addCell($cell45Width, $cellColSpan2)->addText('Title at HKUMed or Faculty of Dentistry:');
$table->addCell($cell6Width);
$table->addRow();
$table->addCell($cell1Width)->addText('Remark:');
$table->addCell($cell2to6Width, $cellColSpan5);
$table->addRow();
$table->addCell($fullWidth, $cellColSpan6)->addText('Part B: Study Particulars');
$table->addRow();
$table->addCell($cell1Width)->addText('Protocol Title:');
$table->addCell($cell2to6Width, $cellColSpan5);
$table->addRow();
$table->addCell($cell1Width)->addText('Disease/Medical Condition to be Studied:');
$table->addCell($cell2to6Width, $cellColSpan5);
$table->addRow();
$table->addCell($cell1Width, $cellRowSpan)->addText('Target No. of Participants:');
$table->addCell($cell2Width, $cellRowSpan);
$table->addCell($cell34Width, $cellColSpan2)->addText('Participant Type:');
$table->addCell($cell56Width, $cellColSpan2)->addText('Patients Healthy volunteers');
$table->addRow();
$table->addCell(null, $cellRowContinue);
$table->addCell(null, $cellRowContinue);
$table->addCell($cell34Width, $cellColSpan2)->addText('Participant Age Range:');
$table->addCell($cell56Width, $cellColSpan2)->addText('Lowest: Highest:');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
// $objWriter->save('helloWorld.docx');
$tempFile = tempnam(sys_get_temp_dir(), 'PHPWord');
$objWriter->save($tempFile);
header("Content-Disposition: attachment; filename=test.docx");
readfile($tempFile);
unlink($tempFile);
Problems:
- The table should have 6 columns, but currently it only has 5.
- The heading "Part A: Principal Investigator" should span 6 columns, but it currently spans only 4 columns.
- The column width seems not correct.
Thank you in advance!


I have modified your code and added one extra row with 6 columns to show that the span is working.
I have also set Margin Left to 0 because you are taking the width of the table as the width of the whole document.
Here is the modified code,
Here is screenshot of output,