Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
943 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...