简体   繁体   English

phpword表格列反转

[英]phpword table column reverse

I am creating table using phpword and add to template.我正在使用 phpword 创建表并添加到模板。 In output file, The table column are reverse in order.在 output 文件中,表格列的顺序是相反的。 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().通过从 addTable() 中删除作为参数的空数组来修复表列顺序。

$tbl = $section->addTable(array());

to

$tbl = $section->addTable();

style name should passed to addTable样式名称应传递给 addTable

$table = $section->addTable([$tableStyle]);

Step to define table style定义表格样式的步骤

  1. create array and define style for table创建数组并定义表的样式

$tableStyle = array( 'borderColor' => '006699', 'borderSize' => 6, 'cellMargin' => 50 ); $tableStyle = array( 'borderColor' => '006699', 'borderSize' => 6, 'cellMargin' => 50 );

  1. define style to phpword object为 phpword 定义样式 object

$phpWord->addTableStyle(' myTable ', $tableStyle ); $phpWord->addTableStyle(' myTable ', $tableStyle );

  1. Add style when create table from section从部分创建表时添加样式

$table = $section->addTable(' myTable '); $table = $section->addTable(' myTable ');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM