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

前端 未结 1 402
别跟我提以往
别跟我提以往 2020-12-16 22:31

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" f

相关标签:
1条回答
  • 2020-12-16 22:53

    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)

    0 讨论(0)
提交回复
热议问题