Quantcast
Channel: PHPWord
Viewing all articles
Browse latest Browse all 450

New Post: Colspan and Rowspan

$
0
0
I write solution!
Try this.....


1. modify file - /Style/Cell.php
(1) add attributes.
/**
     * Horizontal align
     *
     * @var string
     */private $_align;
    /**
     * for the colspan
     */private $_gridSpan;
    /**
     * for the rowspan
     */private $_vMerge;
(2) modify function __construct()
publicfunction __construct() {
        $this->_align = null;       //add this line
        $this->_valign = null;
        $this->_textDirection = null;
        $this->_bgColor = null;
        $this->_borderTopSize = null;
        $this->_borderTopColor = null;
        $this->_borderLeftSize = null;
        $this->_borderLeftColor = null;
        $this->_borderRightSize = null;
        $this->_borderRightColor = null;
        $this->_borderBottomSize = null;
        $this->_borderBottomColor = null;
        $this->_defaultBorderColor = '000000';
        $this->_gridSpan = null;        //add this line
        $this->_vMerge = null;      //add this line
    }
(3) modify function setStyleValue($key, $value)
publicfunction setStyleValue($key, $value) {
        if($key == '_borderSize') {
            $this->setBorderSize($value);
        } elseif($key == '_borderColor') {
            $this->setBorderColor($value);
        } elseif($key == '_gridSpan') {     //add this line
            $this->setGridSpan($value);
        } elseif($key == '_vMerge') {       //add this line
            $this->setVMerge($value);
        } else {
            $this->$key = $value;
        }
    }
(4) add function
publicfunction getAlign() {
        return $this->_align;
    }
    publicfunction setAlign($pValue = null) {
        $this->_align = $pValue;
    }
    publicfunction setGridSpan($pValue = null) {
        $this->_gridSpan = $pValue;
    }
    publicfunction getGridSpan() {
        return $this->_gridSpan;
    }
    publicfunction setVMerge($pValue = null) {
        $this->_vMerge = $pValue;
    }
    publicfunction getVMerge() {
        return $this->_vMerge;
    }
2. modify file - /Writer/Word2007/Base.php
(1) modify function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null)
protectedfunction _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) {
        $bgColor = $style->getBgColor();
        $align = $style->getAlign();        //add this line
        $valign = $style->getVAlign();
        $textDir = $style->getTextDirection();
        $brdSz = $style->getBorderSize();
        $brdCol = $style->getBorderColor();
        $gridSpan = $style->getGridSpan();      //add this line
        $vMerge = $style->getVMerge();      //add this line

        $bTop = (!is_null($brdSz[0])) ? true : false;
        $bLeft = (!is_null($brdSz[1])) ? true : false;
        $bRight = (!is_null($brdSz[2])) ? true : false;
        $bBottom = (!is_null($brdSz[3])) ? true : false;
        $borders = ($bTop || $bLeft || $bRight || $bBottom) ? true : false;

        $styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || !is_null($gridSpan) || !is_null($vMerge) || $borders) ? true : false;       //modify this lineif($styles) {
            if(!is_null($textDir)) {
                $objWriter->startElement('w:textDirection');
                    $objWriter->writeAttribute('w:val', $textDir);
                $objWriter->endElement();
            }

            if(!is_null($bgColor)) {
                $objWriter->startElement('w:shd');
                    $objWriter->writeAttribute('w:val', 'clear');
                    $objWriter->writeAttribute('w:color', 'auto');
                    $objWriter->writeAttribute('w:fill', $bgColor);
                $objWriter->endElement();
            }

            if(!is_null($valign)) {
                $objWriter->startElement('w:vAlign');
                    $objWriter->writeAttribute('w:val', $valign);
                $objWriter->endElement();
            }

            if(!is_null($align)) {      //add this line
                $objWriter->startElement('w:pPr');
                    $objWriter->startElement('w:jc');
                        $objWriter->writeAttribute('w:val', $valign);
                    $objWriter->endElement();
                $objWriter->endElement();
            }

            if(!is_null($gridSpan)) {       //add this line
                $objWriter->startElement('w:gridSpan');
                $objWriter->writeAttribute('w:val', $gridSpan);
                $objWriter->endElement();
            }

            if(!is_null($vMerge)) {     //add this line
                $objWriter->startElement('w:vMerge');
                $objWriter->writeAttribute('w:val', $vMerge);
                $objWriter->endElement();
            }

            if($borders) {
                $_defaultColor = $style->getDefaultBorderColor();

                $objWriter->startElement('w:tcBorders');
                    if($bTop) {
                        if(is_null($brdCol[0])) { $brdCol[0] = $_defaultColor; }
                        $objWriter->startElement('w:top');
                            $objWriter->writeAttribute('w:val', 'single');
                            $objWriter->writeAttribute('w:sz', $brdSz[0]);
                            $objWriter->writeAttribute('w:color', $brdCol[0]);
                        $objWriter->endElement();
                    }

                    if($bLeft) {
                        if(is_null($brdCol[1])) { $brdCol[1] = $_defaultColor; }
                        $objWriter->startElement('w:left');
                            $objWriter->writeAttribute('w:val', 'single');
                            $objWriter->writeAttribute('w:sz', $brdSz[1]);
                            $objWriter->writeAttribute('w:color', $brdCol[1]);
                        $objWriter->endElement();
                    }

                    if($bRight) {
                        if(is_null($brdCol[2])) { $brdCol[2] = $_defaultColor; }
                        $objWriter->startElement('w:right');
                            $objWriter->writeAttribute('w:val', 'single');
                            $objWriter->writeAttribute('w:sz', $brdSz[2]);
                            $objWriter->writeAttribute('w:color', $brdCol[2]);
                        $objWriter->endElement();
                    }

                    if($bBottom) {
                        if(is_null($brdCol[3])) { $brdCol[3] = $_defaultColor; }
                        $objWriter->startElement('w:bottom');
                            $objWriter->writeAttribute('w:val', 'single');
                            $objWriter->writeAttribute('w:sz', $brdSz[3]);
                            $objWriter->writeAttribute('w:color', $brdCol[3]);
                        $objWriter->endElement();
                    }

                $objWriter->endElement();
            }
        }
    }
3. Here is my source.
$table = $section->addTable(array('borderSize'=>6, 'borderColor'=>'e3e3e3', 'cellMarginRight'=>80, 'cellMarginLeft'=>80));

$table->addRow();
$table->addCell(2100, array('vMerge' => 'restart'))->addText('ROWSPAN 5', array('width'=>130, 'height'=>150, 'align'=>'center'));       //rowspan count 1
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Name', array('bold'=>true, 'color'=>'666666'));
$table->addCell(5500, array('gridSpan' => 3))->addText('My Name is Landy Kim', array('size'=>9));       //colspan

$table->addRow();
$table->addCell(2100, array('vMerge' => ''));       //rowspan count 2
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Nationality', array('bold'=>true, 'color'=>'666666'));
$table->addCell(5500, array('gridSpan' => 3))->addText('South Korea', array('size'=>9));        //colspan

$table->addRow();
$table->addCell(2100, array('vMerge' => ''));       //rowspan count 3
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Address', array('bold'=>true, 'color'=>'666666'));
$table->addCell(5500, array('gridSpan' => 3))->addText('Top Secret^^', array('size'=>9));       //colspan

$table->addRow();
$table->addCell(2100, array('vMerge' => ''));       //rowspan count 4
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Tel', array('bold'=>true, 'color'=>'666666'));
$table->addCell(2000)->addText('000-000-0000', array('size'=>9));
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('Mobile', array('bold'=>true, 'color'=>'666666'));
$table->addCell(2000)->addText('000-0000-0000', array('size'=>9));

$table->addRow();
$table->addCell(2100, array('vMerge' => ''));       //rowspan count 5
$table->addCell(1500, array('bgColor'=>'f1f1f1', 'align'=>'center'))->addText('email', array('bold'=>true, 'color'=>'666666'));
$table->addCell(500, array('gridSpan' => 3))->addText('jun981126@hotmail.com', array('size'=>9));       //colspan

Viewing all articles
Browse latest Browse all 450

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>