使用phpWord将2个表对齐在同一行中

时间:2018-03-14 11:24:52

标签: php phpword

我正在寻找一种解决方案,使多个表与phpWord在同一行中对齐。

实际上这段代码给了我那个结果:

$section = $word->createSection();

$table->addRow(200);
$table->addCell(1200)->addText("Col 1");
$table->addCell(1200)->addText("Col 2");

$table->addRow(200);
$table->addCell(1200)->addText("Col 1");
$table->addCell(1200)->addText("Col 2");

$section->addText("");

$table = $section->addTable();

$table->addRow(200);
$table->addCell(1200)->addText("Col 1");
$table->addCell(1200)->addText("Col 2");

$table->addRow(200);
$table->addCell(1200)->addText("Col 1");
$table->addCell(1200)->addText("Col 2");

Actual rending

但我正在寻找这样的结果: Wanted rending

有人有解决方案吗? 我没有找到任何信息,如果这是一个重复的问题,请告诉我。

此致

1 个答案:

答案 0 :(得分:0)

您可以通过创建嵌套表来实现此目的。

第1步:添加包含一行和两列(单元格)的父表

//Create a section
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section_1 = $phpWord->addSection();

//Define your parent table styles
$parent_table_styles = array('valign' => 'center', 'cellMarginRight' => 200);
$phpWord->addTableStyle('parent_table_styles', $parent_table_styles);

//Add a parent table     
$parent_table = $section_1->addTable('parent_table_styles');

//Add a row to the parent table
$parent_table_row_1 = $parent_table->addRow();

//Add a cell to that parent table row
$parent_table_row_1_cell_1 = $parent_table_row_1->addCell();

//Add another cell to that parent table row
$parent_table_row_1_cell_2 = $parent_table_row_1->addCell();

步骤2:在父表单元格中添加子表(嵌套表)

//Define your child table and child table cell styles
$child_table_styles = array('valign' => 'center', 'borderSize' => 6, 'borderColor' => '999999');
$phpWord->addTableStyle('child_table_styles', $child_table_styles);
$child_table_cell_styles = array('valign' => 'center');
$phpWord->addTableStyle('child_table_cell_styles', $child_table_cell_styles);

//Add your first child table inside 'cell_1' of the parent table row
$child_table_1 = $parent_table_row_1_cell_1->addTable('style_child_table');
$child_table_1->addRow(200);
$child_table_1->addCell(1200, $child_table_cell_styles)->addText("Col 1");
$child_table_1->addCell(1200, $child_table_cell_styles)->addText("Col 2");
$child_table_1->addRow(200);
$child_table_1->addCell(1200, $child_table_cell_styles)->addText("Col 1");
$child_table_1->addCell(1200, $child_table_cell_styles)->addText("Col 2");

//Add your second child table inside 'cell_2' of the parent table row
$child_table_2 = $parent_table_row_1_cell_2->addTable('style_child_table');
$child_table_2->addRow(200);
$child_table_2->addCell(1200, $child_table_cell_styles)->addText("Col 1");
$child_table_2->addCell(1200, $child_table_cell_styles)->addText("Col 2");
$child_table_2->addRow(200);
$child_table_2->addCell(1200, $child_table_cell_styles)->addText("Col 1");
$child_table_2->addCell(1200, $child_table_cell_styles)->addText("Col 2");