将 PhpWord 表格自动调整到窗口

Autofit PhpWord table to window

提问人:Premlatha 提问时间:11/17/2023 最后编辑:Premlatha 更新时间:11/17/2023 访问量:25

问:

我想将phpword表自动调整到窗口。所以,我在创建表时添加了。但是,表列在输出 docx 文件中以相反的顺序显示。我不想颠倒列顺序。array('unit' => 'pct', 'width' => 5000)

<?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');

?>

output

我试过使用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

评论


答:

0赞 Gregor Klarič 12/4/2023 #1

取代

$table_style->setUnit(\PhpOffice\PhpWord\Style\Table::WIDTH_PERCENT);

$table_style->setUnit(\PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT);