简体   繁体   中英

how to go new line in cell in table in phpword?

I want to create a word file .docx from my data that exist in database. To do this job I am using a phpword 0.12.0 . I need to draw a table to place data inside it. After that I need to fetch every row from table in database to automatically go in cell of new line. I can do this job with

$section->addTextBreak();

but without table, now how can I do this job in cell inside the table? I am using below code, but it doesn't work.

$area=array();
$axis=array();
$topic=array();
$table->addRow(900);
$table->addCell(2000, $styleCell)->addText(htmlspecialchars('test1'), $fontStyle);
$table->addCell(2000, $styleCell)->addText(htmlspecialchars('test2'), $fontStyle);
$table->addCell(2000, $styleCell)->addText(htmlspecialchars('test3'), $fontStyle);
$table->addCell(2000, $styleCell)->addText(htmlspecialchars('test4'), $fontStyle);
for ($i = 0; $i < 4; $i++) {
      $table->addRow();
      $table->addCell(2000)->addText(htmlspecialchars($topic{$i}),array('name' => 'Segoe UI Semilight'));
      $table->addCell(3000)->addText(htmlspecialchars($axis{$i}),array('rtl' => true,'name' => 'Segoe UI Semilight'));
      $table->addCell(2000)->addText(htmlspecialchars($area{$i}),array('rtl' => true,'name' => 'Segoe UI Semilight'));
      $table->addCell(2000)->addText(htmlspecialchars($i),array('rtl' => true,'name' => 'Segoe UI Semilight'));
}

You must first create a cell object, and then use addText

    $c1=$table->addCell(2000);
    $c1->addText("Cell 1");
    $c1->addText("New line");

As @Milaza pointed out, you can

// define the cell
$cell = $table->addCell(2000);

// add one line
$cell->addText("Cell 1");

// then add another one
$cell->addText("New line");

I think this is annoying, since I have to break a line of text into multiple lines and I have a lot cell text to break. So I created(2016-11-08) a method in PHPWord/src/PhpWord/Element/Cell.php :

/**
 * Add multi-line text
 *
 * @return \PhpOffice\PhpWord\Style\Cell
 */
public function addMultiLineText($text, $fStyle = null, $pStyle = null)
{
    // break from line breaks
    $strArr = explode('\n', $text);

    // add text line together
    foreach ($strArr as $v) {
        $this->addText($v, $fStyle, $pStyle);
    }
    return $this;
}

It breaks text string from \\n mark.

So when you are adding a cell, you can use it like:

// "$fStyle", "$pStyle" are the old "$fStyle", "$pStyle" you pass to old "addText" method
$table->addCell(2000)->addMultiLineText("Line 1\nLine 2", $fStyle, $pStyle);

The output will look like:

Line 1
Line 2

If you do not want to modify the source code, I have already added it in here.

https://github.com/shrekuu/PHPWord

https://packagist.org/packages/shrekuu/phpword

You can just install this one by running:

composer require shrekuu/phpword

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM