|
| 1 | +<?php |
| 2 | + |
| 3 | +error_reporting(E_ALL); |
| 4 | + |
| 5 | +if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) { |
| 6 | + define('EOL', PHP_EOL); |
| 7 | +} else { |
| 8 | + define('EOL', '<br />'); |
| 9 | +} |
| 10 | + |
| 11 | +require_once '../Classes/PHPWord.php'; |
| 12 | + |
| 13 | +// New Word Document |
| 14 | +echo date('H:i:s') , ' Create new PHPWord object' , EOL; |
| 15 | +$PHPWord = new PHPWord(); |
| 16 | +$section = $PHPWord->createSection(); |
| 17 | +$header = array('size' => 16, 'bold' => true); |
| 18 | + |
| 19 | +// 1. Basic table |
| 20 | + |
| 21 | +$rows = 10; |
| 22 | +$cols = 5; |
| 23 | +$section->addText("Basic table", $header); |
| 24 | + |
| 25 | +$table = $section->addTable(); |
| 26 | +for($r = 1; $r <= 8; $r++) { |
| 27 | + $table->addRow(); |
| 28 | + for($c = 1; $c <= 5; $c++) { |
| 29 | + $table->addCell(1750)->addText("Row $r, Cell $c"); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// 2. Advanced table |
| 34 | + |
| 35 | +$section->addTextBreak(1); |
| 36 | +$section->addText("Fancy table", $header); |
| 37 | + |
| 38 | +$styleTable = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80); |
| 39 | +$styleFirstRow = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF'); |
| 40 | +$styleCell = array('valign' => 'center'); |
| 41 | +$styleCellBTLR = array('valign' => 'center', 'textDirection' => PHPWord_Style_Cell::TEXT_DIR_BTLR); |
| 42 | +$fontStyle = array('bold' => true, 'align' => 'center'); |
| 43 | +$PHPWord->addTableStyle('Fancy Table', $styleTable, $styleFirstRow); |
| 44 | +$table = $section->addTable('Fancy Table'); |
| 45 | +$table->addRow(900); |
| 46 | +$table->addCell(2000, $styleCell)->addText('Row 1', $fontStyle); |
| 47 | +$table->addCell(2000, $styleCell)->addText('Row 2', $fontStyle); |
| 48 | +$table->addCell(2000, $styleCell)->addText('Row 3', $fontStyle); |
| 49 | +$table->addCell(2000, $styleCell)->addText('Row 4', $fontStyle); |
| 50 | +$table->addCell(500, $styleCellBTLR)->addText('Row 5', $fontStyle); |
| 51 | +for($i = 1; $i <= 8; $i++) { |
| 52 | + $table->addRow(); |
| 53 | + $table->addCell(2000)->addText("Cell $i"); |
| 54 | + $table->addCell(2000)->addText("Cell $i"); |
| 55 | + $table->addCell(2000)->addText("Cell $i"); |
| 56 | + $table->addCell(2000)->addText("Cell $i"); |
| 57 | + $text = ($i % 2 == 0) ? 'X' : ''; |
| 58 | + $table->addCell(500)->addText($text); |
| 59 | +} |
| 60 | + |
| 61 | +// 3. colspan (gridSpan) and rowspan (vMerge) |
| 62 | + |
| 63 | +$section->addTextBreak(1); |
| 64 | +$section->addText("Table with colspan and rowspan", $header); |
| 65 | + |
| 66 | +$styleTable = array('borderSize' => 6, 'borderColor' => '999999'); |
| 67 | +$cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center'); |
| 68 | +$cellRowContinue = array('vMerge' => 'continue'); |
| 69 | +$cellColSpan = array('gridSpan' => 2, 'valign' => 'center'); |
| 70 | +$cellHCentered = array('align' => 'center'); |
| 71 | +$cellVCentered = array('valign' => 'center'); |
| 72 | + |
| 73 | +$PHPWord->addTableStyle('Colspan Rowspan', $styleTable); |
| 74 | +$table = $section->addTable('Colspan Rowspan'); |
| 75 | +$table->addRow(); |
| 76 | +$table->addCell(2000, $cellRowSpan)->addText('A', null, $cellHCentered); |
| 77 | +$table->addCell(4000, $cellColSpan)->addText('B', null, $cellHCentered); |
| 78 | +$table->addCell(2000, $cellRowSpan)->addText('E', null, $cellHCentered); |
| 79 | +$table->addRow(); |
| 80 | +$table->addCell(null, $cellRowContinue); |
| 81 | +$table->addCell(2000, $cellVCentered)->addText('C', null, $cellHCentered); |
| 82 | +$table->addCell(2000, $cellVCentered)->addText('D', null, $cellHCentered); |
| 83 | +$table->addCell(null, $cellRowContinue); |
| 84 | + |
| 85 | +// Save File |
| 86 | +echo date('H:i:s') , ' Write to Word2007 format' , EOL; |
| 87 | +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); |
| 88 | +$objWriter->save(str_replace('.php', '.docx', __FILE__)); |
| 89 | + |
| 90 | +// echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; |
| 91 | +// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); |
| 92 | +// $objWriter->save(str_replace('.php', '.odt', __FILE__)); |
| 93 | +// |
| 94 | +// echo date('H:i:s') , ' Write to RTF format' , EOL; |
| 95 | +// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); |
| 96 | +// $objWriter->save(str_replace('.php', '.rtf', __FILE__)); |
| 97 | + |
| 98 | +// Echo memory peak usage |
| 99 | +echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; |
| 100 | + |
| 101 | +// Echo done |
| 102 | +echo date('H:i:s') , ' Done writing file' , EOL; |
0 commit comments