I am creating table using phpword and add to template. In output file, The table column are reverse in order. I have create cell correctly.
<?php
$data=[
['cell 1 row 1','cell 2 row 1'],
['cell 1 row 2','cell 2 row 2']
];
//phpword
require_once '../vendor/autoload.php';
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
use PhpOffice\PhpWord\Shared\XMLWriter;
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setName('Times');
$phpWord = new PhpWord();//phpword object
$section = $phpWord->addSection();//section
$tbl = $section->addTable(array(
/*'ltr' => true,*/
/*"layout" => Table::LAYOUT_AUTO,
"width" => 100 * 50, //in word 1% == 50 unit (with in 1/50)
"unit" => TblWidth::PERCENT,
'lineHeight' => 1*/
));
for($i=0;$i < 40;$i++){
$tbl->addRow();
$tbl->addCell(150)->addText("cell 1 row:$i");
$tbl->addCell(150)->addText("cell 2 row:$i");
$tbl->addCell(150)->addText("cell 3 row:$i");
$tbl->addCell(150)->addText("cell 4 row:$i");
//$tbl->addCell(150)->addMultiLineText("cell\n4 row:$i");
}
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY, './', Settings::hasCompatibility());
$containerWriter = new Container($xmlWriter, $section);
$containerWriter->write();
$elXml = $xmlWriter->getData();
// remplace our templateContent
Settings::setOutputEscapingEnabled(false);
$templateProcessor =new TemplateProcessor('testing_tabledirection.docx');
$templateProcessor->setValue('table', $elXml);
Settings::setOutputEscapingEnabled(true);
$pathToSave ='output_tabledirection.docx';
$templateProcessor->saveAs($pathToSave);
?>
The table column order has been fixed by removing empty array as parameter from addTable().
$tbl = $section->addTable(array());
to
$tbl = $section->addTable();
style name should passed to addTable
$table = $section->addTable([$tableStyle]);
Step to define table style
$tableStyle = array( 'borderColor' => '006699', 'borderSize' => 6, 'cellMargin' => 50 );
$phpWord->addTableStyle('myTable', $tableStyle);
$table = $section->addTable('myTable');