簡體   English   中英

phpword表格列反轉

[英]phpword table column reverse

我正在使用 phpword 創建表並添加到模板。 在 output 文件中,表格列的順序是相反的。 我已正確創建單元格。

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

通過從 addTable() 中刪除作為參數的空數組來修復表列順序。

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

$tbl = $section->addTable();

樣式名稱應傳遞給 addTable

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

定義表格樣式的步驟

  1. 創建數組並定義表的樣式

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

  1. 為 phpword 定義樣式 object

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

  1. 從部分創建表時添加樣式

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM