Добавьте 2-3 строки текста в ячейку таблицы с различным форматированием - PullRequest
0 голосов
/ 18 января 2019

Я пытаюсь сделать таблицу используя 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 голосов
/ 19 января 2019

Просто создайте объект ячейки

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