You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHPWord表格gridSpan列合并异常及列数、列宽问题求助

PHPWord表格生成不符合预期的问题及解决方案

问题描述

我使用PHPWord创建表格,预期为6列布局的表格,但实际生成的表格存在以下问题:

  • 表格应包含6列,实际仅显示5列;
  • 标题“Part A: Principal Investigator”应跨6列,实际仅跨4列;
  • 列宽显示不正确。

原代码

$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); 

问题根源

  1. 未定义表格基础列结构:PHPWord的表格合并(gridSpan/vMerge)依赖预先定义的基础列数和列宽,原代码没有初始化6列的基础结构,导致合并逻辑无法正确识别列数。
  2. 合并逻辑无有效基准:所有跨列、跨行操作都需要基于明确的列结构,缺少基础列的情况下,gridSpan参数无法对应正确的列数,导致跨列失效、列宽混乱。

修复方案

1. 初始化表格基础列结构

在创建表格后,先添加一行包含6个对应宽度的单元格(设置行高为0,不显示),作为表格的列结构模板:

$table = $section->addTable([
    'borderColor' => 'cccccc',
    'borderSize'  => 6,
]);
// 初始化6列基础宽度,行高设为0以隐藏该行
$table->addRow(0);
$table->addCell($cell1Width);
$table->addCell($cell2Width);
$table->addCell($cell3Width);
$table->addCell($cell4Width);
$table->addCell($cell5Width);
$table->addCell($cell6Width);

2. 完整修复后的代码

$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,
]);

// 初始化6列基础结构,隐藏该行
$table->addRow(0);
$table->addCell($cell1Width);
$table->addCell($cell2Width);
$table->addCell($cell3Width);
$table->addCell($cell4Width);
$table->addCell($cell5Width);
$table->addCell($cell6Width);

$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');

$tempFile = tempnam(sys_get_temp_dir(), 'PHPWord');
$objWriter->save($tempFile);

header("Content-Disposition: attachment; filename=test.docx");
readfile($tempFile);
unlink($tempFile); 

修复效果说明

  • 基础列结构定义后,gridSpan和vMerge操作有了明确的基准,6列结构可以正确渲染;
  • 标题行的gridSpan=6可以正确跨满整个表格宽度;
  • 列宽计算基于预先定义的基础列,显示效果与预期一致。

内容的提问来源于stack exchange,提问作者Wilson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 06:34:56