zoukankan      html  css  js  c++  java
  • PHPWord生成word实现table合并(colspan和rowspan)

    PHPWord(http://phpword.codeplex.com/)是一个很好处理和生成WORD文档的工具,但是生成复杂的word,如colspan和rowspan的实现,还是需要你做些修改。

    第一步:在phpword/Style/Cell.php文件类中添加如下属性:

    private $_gridSpan;// for the colspan
    private $_vMerge;// for the rowspan

    第二步:在phpword/Style/Cell.php文件类中添加如下方法:

    public function setGridSpan($pValue = null) 
    { 
       $this->_gridSpan = $pValue; 
    } 
    public function getGridSpan() 
    { 
       return $this->_gridSpan; 
    }
    public function setVMerge($pValue = null) 
    { 
       $this->_vMerge = $pValue; 
    } 
    public function getVMerge() 
    { 
       return $this->_vMerge; 
    }


    第三步:在phpword/Style/Cell.php文件类构造函数__construct()中添加如下:

    $this->_gridSpan=null;
    $this->_vMerge=null;

    第四步:在phpword/writer/word2007/base.php类的_writeCellStyle方法中添加:

    $gridSpan = $style->getGridSpan();
    if(!is_null($gridSpan)) 
    { 
        $objWriter->startElement('w:gridSpan'); 
        $objWriter->writeAttribute('w:val', $gridSpan); 
        $objWriter->endElement(); 
    }
    /** edited by www.phpddt.com */
    $vMerge = $style->getVMerge(); 
    if(!is_null($vMerge)) 
    { 
        $objWriter->startElement('w:vMerge'); 
        $objWriter->writeAttribute('w:val', $vMerge); 
        $objWriter->endElement(); 
    }

    OK,恭喜你,搞定了,然后看看怎么使用吧!

    PHPWord rowspan的使用:

    $table = $section->addTable();
    $table->addRow();
    $table->addCell(100,array('vMerge' => 'restart'))->addText('1');
    $table->addCell(100)->addText('2');
    $table->addRow();
    $table->addCell(100,array('vMerge' => 'fusion'));
    $table->addCell(100)->addText('3');

     生成的word效果如下截图:

     

    PHPWord colspan的使用:

            $table->addRow();
            $styleCell=array('gridSpan' => 2);
            $table->addCell(200, $styleCell)->addText('PHP点点通');
            $table->addCell(100)->addText('http://www.phpddt.com');
            $table->addRow();
            $table->addCell(100)->addText('PHP');
            $table->addCell(100)->addText('python');
            $table->addCell(100)->addText('java');
            $section->addTextBreak(10);

    生成word效果图如下:

  • 相关阅读:
    基于Haproxy+Keepalived构建高可用负载均衡集群
    基于 Haproxy 构建负载均衡集群
    shell for循环练习题99乘法表
    帮软件同事写的vsftpd服务虚拟用户管理脚本
    sed文件处理练习题
    判断ssh登录密码验证错误超过5次的IP被拉黑
    使用shell中数组功能生成自己的手机号
    利用Crontab设置每个月第一个周六的17:30执行/opt/shell.sh 脚本
    Tomcat 项目代码上线步骤详解
    Jar/War/Ear等包的作用与区别详解
  • 原文地址:https://www.cnblogs.com/ahwu/p/4366521.html
Copyright © 2011-2022 走看看