将 PhpWord 表格自动调整到窗口

问题描述 投票:0回答:1

我想将 phpword 表自动调整到窗口。所以,我在创建表格时添加了

array('unit' => 'pct', 'width' => 5000)
。但是,表列在输出 docx 文件中以相反的顺序显示。我不想颠倒列顺序。

<?php
 include_once '../vendor/autoload.php';
 $phpWord = new \PhpOffice\PhpWord\PhpWord();
 use PhpOffice\PhpWord\Element\Table;
 use PhpOffice\PhpWord\TemplateProcessor;
 use PhpOffice\PhpWord\ComplexType\TblWidth;
 use PhpOffice\PhpWord\Settings;
 use PhpOffice\PhpWord\SimpleType\Border;
 //$styleCell = array('borderColor' =>'black', 'borderSize' => 2);
 //$fontStyle=array('align'=>'center');

 $section = $phpWord->addSection();
 $table = $section->addTable(array('unit' => 'pct', 'width' => 5000));
//$table = new Table();

$table->addRow();
$table->addCell(700)->addText('cell 1');
$table->addCell(500)->addText('cell 2');

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('1711output.docx');

?>

我尝试过使用 setWidth 方法

$table_style = new \PhpOffice\PhpWord\Style\Table;
$table_style->setUnit(\PhpOffice\PhpWord\Style\Table::WIDTH_PERCENT);
$table_style->setWidth(100 * 50);
$table = $section->addTable($table_style);

我有错误

 Uncaught Error: Undefined class constant 'WIDTH_PERCENT'

php phpword
1个回答
0
投票

更换 $table_style->setUnit(\PhpOffice\PhpWord\Style\Table::WIDTH_PERCENT); 和 $table_style->setUnit(\PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT);

© www.soinside.com 2019 - 2024. All rights reserved.