php - 表格标题每页Word PHPWord

标签 php each phpword

我正在使用 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');
?>

我的问题是,是否有比这更好的选择将表格标题保留在每个页面中?

结果:
enter image description here

最佳答案

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

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

关于php - 表格标题每页Word PHPWord,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39241401/

相关文章:

jquery - 将图像宽度增加 50px

php - 如何使用 phpword 将页脚拉伸(stretch)到页面的整个宽度?

php - 用PHP打印word文件

php - 如何正确使用Flysystem和Local Adapter

php - 向我们传输文件 NuSOAP

Jquery every - 停止循环并返回对象

ruby - 在 "each"循环中覆盖数组元素

php - CodeIgniter PHPWord 作为第三方使用

PHPWORD 如何在不中断的情况下使用函数 addText

php - AJAX post后如何实现302重定向?尝试实现 prg-pattern