【问题标题】:Align 2 tables in same line with phpWord将 2 个表格与 phpWord 对齐在同一行
【发布时间】:2018-08-22 21:52:47
【问题描述】:

我正在寻找一种解决方案,将多个表格与 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");

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

有人有解决办法吗? 我没有找到任何信息,如果它是重复的问题,请告诉我。

问候,

【问题讨论】:

  • 最简单的方法是把你的桌子放在一张桌子上;)
  • 我知道该解决方案但无法使用它,正在寻找其他方法来制作它。问题是当你编辑你的word文档时,你可以看到父表的属性。我必须找到没有父表的解决方案。或任何其他对象。父母必须是文件。就像您将表格拖放到 word doc 上以使其与第一个表格对齐时一样
  • 据我所知,这在 PHPWord 中是不可能的。当您在 Word 中拖放表格并签出 Word-XML(只需解压缩 .docx 文件)时,您将看到带有表格样式属性的 <w:tblPr> 标记(在本例中,类似于表格网格)。我不认为 PHPWord 能够处理那种特殊的风格。

标签: php phpword


【解决方案1】:

您可以通过创建嵌套表来实现这一点。

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

【讨论】:

  • 感谢您的回答,但我正在尝试找到没有父对象的解决方案(每个表的直接父对象必须是一个部分)。
  • @Paulo 为什么需要将文档作为这两个表的直接父级?
  • 客户需要,能够编辑文档。但是我们改变了数据结构,所以现在不再有问题了,但仍在寻找解决方案,我想我会尝试在 phpWord 中为标志“”实现扩展。但是到月底有点忙。如果我做“有趣”和“好”的事情,我会保持更新这篇文章
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 2012-07-28
  • 1970-01-01
相关资源
最近更新 更多