PHPword 表格内换行处理

PHPWord文本换行很简单:

$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$section->addText('php点点通-www.phpddt.com');
$section->addTextBreak(1);//换行

但是在添加table的cell单元格中换行,好像没有相应的方法,提供一个PHPWord实现table cell单元格内容换行解决方案如下:
把phpword/writer/word2007/base.php中_writeText()方法替换如下:

protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false) {
        $styleFont = $text->getFontStyle();
        $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
        //@blog<www.phpddt.com>
        $strText = htmlspecialchars($text->getText());
        // 将文本用/n分割成数组
                $str_tmp=explode("/n",$strText);
                // 循环输出数组文本
                for ($i = 0; $i < count($str_tmp); $i++) {
                    if(!$withoutP) {
                        $objWriter->startElement('w:p');
                        $styleParagraph = $text->getParagraphStyle();
                        $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;

                        if($SpIsObject) {
                            $this->_writeParagraphStyle($objWriter, $styleParagraph);
                        } elseif(!$SpIsObject && !is_null($styleParagraph)) {
                            $objWriter->startElement('w:pPr');
                            $objWriter->startElement('w:pStyle');
                            $objWriter->writeAttribute('w:val', $styleParagraph);
                            $objWriter->endElement();
                            $objWriter->endElement();
                        }
                    }
                    $strText = htmlspecialchars($text->getText());
                    $strText = PHPWord_Shared_String::ControlCharacterPHP2OOXML($strText);
                    $strText = $str_tmp[$i];
                    $strText = PHPWord_Shared_String::ControlCharacterPHP2OOXML($strText);
                    $objWriter->startElement('w:r');

                    if($SfIsObject) {
                        $this->_writeTextStyle($objWriter, $styleFont);
                    } elseif(!$SfIsObject && !is_null($styleFont)) {
                        $objWriter->startElement('w:rPr');
                        $objWriter->startElement('w:rStyle');
                        $objWriter->writeAttribute('w:val', $styleFont);
                        $objWriter->endElement();
                        $objWriter->endElement();
                    }

                    $objWriter->startElement('w:t');
                    $objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text
                    $objWriter->writeRaw($strText);
                    $objWriter->endElement();
                    $objWriter->endElement(); // w:r
                    if(!$withoutP) {
                        $objWriter->endElement(); // w:p
                    }
                }
    }

只要在表格内容里面 加上 /n就可以换行了!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值