PHP Classes

File: examples/table2.php

Recommend this page to a friend!
  Classes of Raskin Veniamin   VS PHP Word HTML   examples/table2.php   Download  
File: examples/table2.php
Role: Example script
Content type: text/plain
Description: Example script
Class: VS PHP Word HTML
Create DOCX Word document dynamically from HTML
Author: By
Last change: fix bags
Date: 9 years ago
Size: 2,035 bytes
 

Contents

Class file image Download
<?php
/**
* The example creates a table from an array
*/

require_once '../vsword/VsWord.php';
VsWord::autoLoad();

$data = array(
    array(
       
"Name"=>"A. Pushkin",
       
"Age"=>"31",
       
"Phone"=>"none",
       
"Address"=>"SPb, pr. Moyki 17",
       
"Mail"=>"none",
    ),
    array(
       
"Name"=>"M. Ivanov",
       
"Age"=>"54",
       
"Phone"=>"521-8798",
       
"Address"=>"Moskov, pr. Lenina 56",
       
"Mail"=>"m.ivanov@info.com",
    ),
    array(
       
"Name"=>"M. Chernova",
       
"Age"=>"23",
       
"Phone"=>"+7-911-7865421",
       
"Address"=>"Penza, pr. Lenina 12",
       
"Mail"=>"none",
    ),
    array(
       
"Name"=>"V. Ut",
       
"Age"=>"34",
       
"Phone"=>"none",
       
"Address"=>"SPb, pr. Lenina 12",
       
"Mail"=>"none",
    ),
);



$doc = new VsWord();
$body = $doc->getDocument()->getBody();

$table = new TableCompositeNode();
$body->addNode($table);
$style = new TableStyle(1);
$table->setStyle($style);
$doc->getStyle()->addStyle($style);

$first = TRUE;
foreach(
$data as $item) {

    if(
$first) {//add header
       
$tr = new TableRowCompositeNode();
       
$table->addNode($tr);
        foreach(
$item as $key=>$value) {
           
$col = new TableColCompositeNode();
           
$tr->addNode($col);
            
           
$rTitle = new RCompositeNode();
           
$col->getLastPCompositeNode()->addNode($rTitle);
           
$rTitle->addTextStyle(new BoldStyleNode());
           
$rTitle->addTextStyle(new FontSizeStyleNode(18));
           
$rTitle->addText($key);
           
        }
       
$first = false;
    }

   
$tr = new TableRowCompositeNode();
   
$table->addNode($tr);
    foreach(
$item as $key=>$value) {
   
        if(
$key == "Mail"){
           
$col = new TableColCompositeNode();
           
$tr->addNode($col);
           
$link = new HyperlinkCompositeNode();
           
$rLink = new RCompositeNode();
           
$rLink->addText($value);
           
$link->addNode($rLink);
           
$col->getLastPCompositeNode()->addNode( $link );
           
$col->getLastPCompositeNode()->addNode( new BrNode() );
        } else {
           
$col = new TableColCompositeNode();
           
$col->addText($value);
           
$col->getLastPCompositeNode()->addNode( new BrNode() );
           
$tr->addNode($col);
        }
    }
}

echo
'<pre>'.($doc->getDocument()->getBody()->look()).'</pre>';

$doc->saveAs('./table2.docx');