add exmpales
[pear-examples] / Document_Word_Writer / Examples / AdvancedTable.php
1 <?php
2 ini_set('include_path', __DIR__  . '/../../..:.');
3 require_once __DIR__ . '/../Writer.php';
4 require_once __DIR__ . '/../Writer/Style/Cell.php';
5
6 // New Word Document
7 $PHPWord = new Document_Word_Writer();
8
9 // New portrait section
10 $section = $PHPWord->createSection();
11
12 // Define table style arrays
13 $styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
14 $styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');
15
16 // Define cell style arrays
17 $styleCell = array('valign'=>'center');
18 $styleCellBTLR = array('valign'=>'center', 'textDirection'=>Document_Word_Writer_Style_Cell::TEXT_DIR_BTLR);
19
20 // Define font style for first row
21 $fontStyle = array('bold'=>true, 'align'=>'center');
22
23 // Add table style
24 $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
25
26 // Add table
27 $table = $section->addTable('myOwnTableStyle');
28
29 // Add row
30 $table->addRow(900);
31
32 // Add cells
33 $table->addCell(2000, $styleCell)->addText('Row 1', $fontStyle);
34 $table->addCell(2000, $styleCell)->addText('Row 2', $fontStyle);
35 $table->addCell(2000, $styleCell)->addText('Row 3', $fontStyle);
36 $table->addCell(2000, $styleCell)->addText('Row 4', $fontStyle);
37 $table->addCell(500, $styleCellBTLR)->addText('Row 5', $fontStyle);
38
39 // Add more rows / cells
40 for($i = 1; $i <= 10; $i++) {
41         $table->addRow();
42         $table->addCell(2000)->addText("Cell $i");
43         $table->addCell(2000)->addText("Cell $i");
44         $table->addCell(2000)->addText("Cell $i");
45         $table->addCell(2000)->addText("Cell $i");
46         
47         $text = ($i % 2 == 0) ? 'X' : '';
48         $table->addCell(500)->addText($text);
49 }
50
51
52 // Save File
53 require_once __DIR__ . '/../Writer/IOFactory.php';
54 $objWriter = Document_Word_Writer_IOFactory::createWriter($PHPWord, 'Word2007');
55 $objWriter->save('/tmp/AdvancedTable.docx');
56 ?>