将2-3行文本添加到具有不同格式的表格单元格

时间:2019-01-18 13:23:52

标签: php phpword

我尝试使用phpWord制作表格。但是我有一个问题-每个单元格中表格的一列必须具有不同格式的文本:

example

首先,我尝试将其中的3行做成困难的行

  _   _   _   _
|   | _ |   |   | - row1 of row
|   | _ |   |   | - row2 of row
| _ | _ | _ | _ | - row3 of row

但是我需要禁止PHPWord破坏页面之间的表行。

$table->addRow(null, array('tblHeader' => true, 'cantSplit' => true));

我的行包含3行,因此它不起作用。因此,只有一种方法-使用一个单元格并在其中包含所有文本(看图片)。

如何添加具有不同格式的单元格文本?

public function test2()
    {
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $section = $phpWord->addSection();
        $table = $section->addTable();
        for ($r = 1; $r <= 80; $r++) {
            $table->addRow(null, array('tblHeader' => false, 'cantSplit' => true));
            for ($c = 1; $c <= 10; $c++) {
                $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et ultricies orci. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin vehicula lorem ac sodales ullamcorper.';
                $table->addCell(1750)->addText($text);
            }
        }

        $this->output_file($phpWord, 'tt');
    }

private function output_file($phpWord, $name)
    {
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $name . '.docx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $xmlWriter->save("php://output");
    }

PhpWord版本-最新稳定版(0.16.0)

1 个答案:

答案 0 :(得分:1)

只需创建单元格对象

$c1 = $row->addCell(100);
$c1->addText('1', ['bold' => true]);
$c1->addText('2');
$c1->addText('3', ['italic' => true]);
相关问题