PHPWord 表格列跨度(gridSpan)无法按预期工作

问题描述 投票:0回答:1

我正在使用 PHPWord 创建一个这样的表:

但是我得到了这个:

我的代码:

    $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. 表格应该有 6 列,但目前只有 5 列。
  2. 标题“A 部分:首席研究员”应跨 6 栏,但目前仅跨 4 栏。
  3. 列宽似乎不正确。

提前谢谢您!

php laravel phpword phpoffice
1个回答
0
投票
  1. 它有 6 列我已经在 Google Docs 中检查过了
  2. 标题也跨越 6 列
  3. 由于最后一行中的空值和默认边距,列的宽度不正确

我修改了您的代码并添加了一行 6 列以显示跨度正在运行。

我还将 Margin Left 设置为 0,因为您将表格的宽度作为整个文档的宽度。

这是修改后的代码,

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection(['marginLeft' => 0]);

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

$bgColor = [ 'bgColor' => 'aabbcc'];

$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($cell1Width, $cellRowContinue);
$table->addCell($cell2Width, $cellRowContinue);
$table->addCell($cell34Width, $cellColSpan2)->addText('Participant Age Range:');
$table->addCell($cell56Width, $cellColSpan2)->addText('Lowest:  Highest:');

$table->addRow();
$table->addCell($cell1Width, $bgColor)->addText('Col 1');
$table->addCell($cell2Width, $bgColor)->addText('Col 2');
$table->addCell($cell3Width, $bgColor)->addText('Col 3');
$table->addCell($cell4Width, $bgColor)->addText('Col 4');
$table->addCell($cell5Width, $bgColor)->addText('Col 5');
$table->addCell($cell6Width, $bgColor)->addText('Col 6');


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

这是输出的屏幕截图,

© www.soinside.com 2019 - 2024. All rights reserved.