【问题标题】:Table Headers each page Word PHPWord每个页面的表头 Word PHPWord
【发布时间】:2017-01-07 13:25:53
【问题描述】:

我正在使用 PHPWord 生成报告,但我没有找到在每个页面中保留表格标题的方法。问题是我无法为单元格赋予样式,并且标题和主表之间有一些空间。

我试试这个,但我认为这不是最好的解决方案:

<?php
require_once 'PHPWord.php';

// New Word Document
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Define table style arrays
$styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
$styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');

// Define cell style arrays
$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);

// Define font style for first row
$fontStyle = array('bold'=>true, 'align'=>'center');

// Add table style
$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
$header = $section->createHeader();
$table = $header->addTable();
$table->addRow(900);
$table->addCell(2000,$styleCell)->addText('Row1',$fontStyle);
$table->addCell(2000,$styleCell)->addText('Row2',$fontStyle);
$table->addCell(2000,$styleCell)->addText('Row3',$fontStyle);
$table->addCell(2000,$styleCell)->addText('Row4',$fontStyle);
$table->addCell(500,$styleCell)->addText('Row5',$fontStyle);
// Add table
$table = $section->addTable('myOwnTableStyle');

// Add more rows / cells
for($i = 1; $i <= 1000; $i++) {
    $table->addRow();
    $table->addCell(2000)->addText("Cell $i");
    $table->addCell(2000)->addText("Cell $i");
    $table->addCell(2000)->addText("Cell $i");
    $table->addCell(2000)->addText("Cell $i");

    $text = ($i % 2 == 0) ? 'X' : '';
    $table->addCell(500)->addText($text);
}


// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('AdvancedTable.docx');
?>

我的问题是,有没有比这更好的选择来保留每个页面中的表头?

结果:

【问题讨论】:

    标签: php each phpword


    【解决方案1】:

    在 PhpWord 0.13.0 中,您可以使用行参数 tblHeader 定义在每个新页面上重复的表头行:

    // your header row addition
    $table->addRow(900, array('tblHeader' => true));
    

    【讨论】:

    • 别忘了提一下,如果你的表头涉及多行,你必须把那个数组添加到所有的行中。
    猜你喜欢
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多