gpt4 book ai didi

php - 将 2 个表与 phpWord 对齐

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:42 31 4
gpt4 key购买 nike

我正在寻找一种解决方案,以使用 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 步:添加一个包含一行两列(单元格)的父表

//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");

关于php - 将 2 个表与 phpWord 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49276541/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com