Table in a cell?

Feb 13, 2011 at 10:43 AM

Hi!

How can I create a table in a cell?
 

MFG
derSESO 

Feb 13, 2011 at 2:43 PM
Edited Feb 13, 2011 at 2:43 PM

Nested tables are not supported at this time. http://phpword.codeplex.com/discussions/216740

Feb 13, 2011 at 4:40 PM

Great - thanks for the tip. :)

Sep 25, 2013 at 11:53 AM
I got this working , please try

In PHPword/Writer/Word2007/base.php


protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) {
    $_rows = $table->getRows();
    $_cRows = count($_rows);

    if($_cRows > 0) {
        $objWriter->startElement('w:tbl');
            $tblStyle = $table->getStyle();
            if($tblStyle instanceof PHPWord_Style_Table) {
                $this->_writeTableStyle($objWriter, $tblStyle);
            } else {
                if(!empty($tblStyle)) {
                    $objWriter->startElement('w:tblPr');
                        $objWriter->startElement('w:tblStyle');
                            $objWriter->writeAttribute('w:val', $tblStyle);
                        $objWriter->endElement();
                    $objWriter->endElement();
                }
            }

            $_heights = $table->getRowHeights();
            for($i=0; $i<$_cRows; $i++) {
                $row = $_rows[$i];
                $height = $_heights[$i];

                $objWriter->startElement('w:tr');

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

                    foreach($row as $cell) {
                        $objWriter->startElement('w:tc');

                            $cellStyle = $cell->getStyle();
                            $width = $cell->getWidth();

                            $objWriter->startElement('w:tcPr');
                                $objWriter->startElement('w:tcW');
                                    $objWriter->writeAttribute('w:w', $width);
                                    $objWriter->writeAttribute('w:type', 'dxa');
                                $objWriter->endElement();

                                if($cellStyle instanceof PHPWord_Style_Cell) {
                                    $this->_writeCellStyle($objWriter, $cellStyle);
                                }

                            $objWriter->endElement();

                            $_elements = $cell->getElements();
                            if(count($_elements) > 0) {
                                foreach($_elements as $element) {
                                    if($element instanceof PHPWord_Section_Text) {
                                        $this->_writeText($objWriter, $element);
                                    } elseif($element instanceof PHPWord_Section_TextRun) {
                                        $this->_writeTextRun($objWriter, $element);
                                    } elseif($element instanceof PHPWord_Section_Link) {
                                        $this->_writeLink($objWriter, $element);
//ADD
                                        }elseif($element instanceof PHPWord_Section_Table) {
                                              $this->_writeTable($objWriter, $element);
//
                                    } __elseif($element instanceof PHPWord_Section_TextBreak) {
                                        $this->_writeTextBreak($objWriter);
                                    } elseif($element instanceof PHPWord_Section_ListItem) {
                                        $this->_writeListItem($objWriter, $element);
                                    } elseif($element instanceof PHPWord_Section_Image ||
                                             $element instanceof PHPWord_Section_MemoryImage) {
                                        $this->_writeImage($objWriter, $element);
                                    } elseif($element instanceof PHPWord_Section_Object) {
                                        $this->_writeObject($objWriter, $element);
                                    } elseif($element instanceof PHPWord_Section_Footer_PreserveText) {
                                        $this->_writePreserveText($objWriter, $element);
                                    }
                                }
                            } else {
                                $this->_writeTextBreak($objWriter);
                            }

                        $objWriter->endElement();
                    }
                $objWriter->endElement();
            }
        $objWriter->endElement();
    }
}
and see next post
Sep 25, 2013 at 11:56 AM
In PhpWord/Section/Table/Cell.php

//Add this at the end of class PHPWord_Section_Table_Cell
public function addTable($style = null) {
    $table = new PHPWord_Section_Table('table', $this->_pCount, $style);
    $this->_elementCollection[] = $table;
    return $table;
}
Thanks
Feb 21, 2014 at 4:43 PM
Theppadon,

Thanks for the code revisions, but you didn't give us any hints on how to implement it. Care to share?
Feb 28, 2014 at 3:47 AM
Bill_VA wrote:
Theppadon,

Thanks for the code revisions, but you didn't give us any hints on how to implement it. Care to share?
Hi Bill

Once you modify the code. You can add Table to cell same way when you add table from section.



/////////////////Create fest TABLE////////////////////////
$table = $section->addTable(array('width' => 900, 'cellMarginTop' => 100,
'cellMarginLeft' => 0,
'cellMarginRight' => 80,
'cellMarginBottom' => 80)
);
$textStyleSmall = array('color' => '000000', 'size' => 9);
$table->addRow();
$imageStyle = array('width' => 330, 'height' => 890, 'marginBottom' => 100, 'align' => 'left');

////////////////////////////////////// CREATE CELL /////////////////////////////////////
$cell = $table->addCell(300);
$cell->addImage($WORD_TEMPLATE_IMAGE_PATH . 'image3.jpeg', $imageStyle);
$table->addCell(200);

$cell = $table->addCell(10000);
$cell->addImage($WORD_TEMPLATE_IMAGE_PATH . 'image4.jpg', array('width' => 205, 'align' => 'left'));
$textStyle = array('color' => '559BA0', 'size' => 15);
$cell->addText('Leading the way', $textStyle);
$cell->addText('GWA Bathrooms & Kitchens is at the forefront of product innovation, incorporating a world-class R&D centre focused on developing the latest water saving, energy-efficient, and environmentally friendly technology', $textStyleSmall);

$cell->addText('Over the years we have achieved an impressive list of high achieving world firsts:', $textStyleSmall);
$PHPWord->addFontStyle('ItemListStyle', array('size' => 8, 'color' => 'FF0000'));
$PHPWord->addParagraphStyle('pNormal', array('size' => 8, 'align' => 'left', 'spaceBefore' => 0,
'spaceAfter' => 0, 'spacing' => 0));
$listTable_style = array('cellMarginTop' => 0,
'cellMarginLeft' => 0,
'cellMarginRight' => 0,
'cellMarginBottom' => 0);
$PHPWord->addParagraphStyle('listTableTextParaStyle', array('align' => 'left',
'spaceBefore' => 0, 'spaceAfter' => 120, 'spacing' => 1));


////////////////////////////////// THEN CREATE TABLE IN A CELL //////////////////////////////////////////
$cellTable1 = $cell->addTable($listTable_style);
$cellTable1->addRow();
Mar 14, 2014 at 3:42 PM
We're close, but not quite. Using the following code I get a file formatted just the way I want, but Word gives me an error when I open the file. After I dismiss the error, it displays correctly. What am I missing? I made all the code changes you suggested.
$section = $PHPWord->createSection();
$table_block_format = array( 'borderSize' =>  7,
                                'cellMarginTop' =>  0,
                                'cellMarginLeft' =>  100,
                                'cellMarginRight' =>  100,
                                'cellMarginBottom' =>  0,
                                'valign' =>  'top',
                          );
$PHPWord->addTableStyle('myTableBorder', $table_block_format);

$table1 = $section->addTable('myTableBorder');
    $table1->addRow();
        $table1->addCell(3750)->addText("test");
        $table1->addCell(3750)->addText("test");
        $table1->addCell(3750)->addText("test");
    $table1->addRow();
        $table1->addCell(3750)->addText("test");        
        $cell1 = $table1->addCell(3750); 
            $cellTable1 = $cell1->addTable('myTableBorder');
            $cellTable1->addRow();
                $cellTable1->addCell(750)->addText("1");
                $cellTable1->addCell(750)->addText("2");
                $cellTable1->addCell(750)->addText("3");            
        $table1->addCell(3750)->addText("test");
    $table1->addRow();
        $table1->addCell(3750)->addText("test");
        $table1->addCell(3750)->addText("test");
        $table1->addCell(3750)->addText("test");
Mar 20, 2014 at 3:55 AM
Hi all. I've posted the request to GitHub. Nested table should be available on 0.9.