AdvancedTable.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. require_once '../PHPWord.php';
  3. // New Word Document
  4. $PHPWord = new PHPWord();
  5. // New portrait section
  6. $section = $PHPWord->createSection();
  7. // Define table style arrays
  8. $styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
  9. $styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');
  10. // Define cell style arrays
  11. $styleCell = array('valign'=>'center');
  12. $styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
  13. // Define font style for first row
  14. $fontStyle = array('bold'=>true, 'align'=>'center');
  15. // Add table style
  16. $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
  17. // Add table
  18. $table = $section->addTable('myOwnTableStyle');
  19. // Add row
  20. $table->addRow(900);
  21. // Add cells
  22. $table->addCell(2000, $styleCell)->addText('Row 1', $fontStyle);
  23. $table->addCell(2000, $styleCell)->addText('Row 2', $fontStyle);
  24. $table->addCell(2000, $styleCell)->addText('Row 3', $fontStyle);
  25. $table->addCell(2000, $styleCell)->addText('Row 4', $fontStyle);
  26. $table->addCell(500, $styleCellBTLR)->addText('Row 5', $fontStyle);
  27. // Add more rows / cells
  28. for($i = 1; $i <= 10; $i++) {
  29. $table->addRow();
  30. $table->addCell(2000)->addText("Cell $i");
  31. $table->addCell(2000)->addText("Cell $i");
  32. $table->addCell(2000)->addText("Cell $i");
  33. $table->addCell(2000)->addText("Cell $i");
  34. $text = ($i % 2 == 0) ? 'X' : '';
  35. $table->addCell(500)->addText($text);
  36. }
  37. // Save File
  38. $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
  39. $objWriter->save('AdvancedTable.docx');
  40. ?>