Nessuna descrizione

Sample_09_Tables.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. include_once 'Sample_Header.php';
  3. // New Word Document
  4. echo date('H:i:s'), ' Create new PhpWord object', EOL;
  5. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  6. $section = $phpWord->addSection();
  7. $header = array('size' => 16, 'bold' => true);
  8. // 1. Basic table
  9. $rows = 10;
  10. $cols = 5;
  11. $section->addText(htmlspecialchars('Basic table'), $header);
  12. $table = $section->addTable();
  13. for ($r = 1; $r <= 8; $r++) {
  14. $table->addRow();
  15. for ($c = 1; $c <= 5; $c++) {
  16. $table->addCell(1750)->addText(htmlspecialchars("Row {$r}, Cell {$c}"));
  17. }
  18. }
  19. // 2. Advanced table
  20. $section->addTextBreak(1);
  21. $section->addText(htmlspecialchars('Fancy table'), $header);
  22. $styleTable = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80);
  23. $styleFirstRow = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');
  24. $styleCell = array('valign' => 'center');
  25. $styleCellBTLR = array('valign' => 'center', 'textDirection' => \PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR);
  26. $fontStyle = array('bold' => true, 'align' => 'center');
  27. $phpWord->addTableStyle('Fancy Table', $styleTable, $styleFirstRow);
  28. $table = $section->addTable('Fancy Table');
  29. $table->addRow(900);
  30. $table->addCell(2000, $styleCell)->addText(htmlspecialchars('Row 1'), $fontStyle);
  31. $table->addCell(2000, $styleCell)->addText(htmlspecialchars('Row 2'), $fontStyle);
  32. $table->addCell(2000, $styleCell)->addText(htmlspecialchars('Row 3'), $fontStyle);
  33. $table->addCell(2000, $styleCell)->addText(htmlspecialchars('Row 4'), $fontStyle);
  34. $table->addCell(500, $styleCellBTLR)->addText(htmlspecialchars('Row 5'), $fontStyle);
  35. for ($i = 1; $i <= 8; $i++) {
  36. $table->addRow();
  37. $table->addCell(2000)->addText(htmlspecialchars("Cell {$i}"));
  38. $table->addCell(2000)->addText(htmlspecialchars("Cell {$i}"));
  39. $table->addCell(2000)->addText(htmlspecialchars("Cell {$i}"));
  40. $table->addCell(2000)->addText(htmlspecialchars("Cell {$i}"));
  41. $text = (0== $i % 2) ? 'X' : '';
  42. $table->addCell(500)->addText(htmlspecialchars($text));
  43. }
  44. // 3. colspan (gridSpan) and rowspan (vMerge)
  45. $section->addPageBreak();
  46. $section->addText(htmlspecialchars('Table with colspan and rowspan'), $header);
  47. $styleTable = array('borderSize' => 6, 'borderColor' => '999999');
  48. $cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center', 'bgColor' => 'FFFF00');
  49. $cellRowContinue = array('vMerge' => 'continue');
  50. $cellColSpan = array('gridSpan' => 2, 'valign' => 'center');
  51. $cellHCentered = array('align' => 'center');
  52. $cellVCentered = array('valign' => 'center');
  53. $phpWord->addTableStyle('Colspan Rowspan', $styleTable);
  54. $table = $section->addTable('Colspan Rowspan');
  55. $table->addRow();
  56. $cell1 = $table->addCell(2000, $cellRowSpan);
  57. $textrun1 = $cell1->addTextRun($cellHCentered);
  58. $textrun1->addText(htmlspecialchars('A'));
  59. $textrun1->addFootnote()->addText(htmlspecialchars('Row span'));
  60. $cell2 = $table->addCell(4000, $cellColSpan);
  61. $textrun2 = $cell2->addTextRun($cellHCentered);
  62. $textrun2->addText(htmlspecialchars('B'));
  63. $textrun2->addFootnote()->addText(htmlspecialchars('Colspan span'));
  64. $table->addCell(2000, $cellRowSpan)->addText(htmlspecialchars('E'), null, $cellHCentered);
  65. $table->addRow();
  66. $table->addCell(null, $cellRowContinue);
  67. $table->addCell(2000, $cellVCentered)->addText(htmlspecialchars('C'), null, $cellHCentered);
  68. $table->addCell(2000, $cellVCentered)->addText(htmlspecialchars('D'), null, $cellHCentered);
  69. $table->addCell(null, $cellRowContinue);
  70. // 4. Nested table
  71. $section->addTextBreak(2);
  72. $section->addText(htmlspecialchars('Nested table in a centered and 50% width table.'), $header);
  73. $table = $section->addTable(array('width' => 50 * 50, 'unit' => 'pct', 'align' => 'center'));
  74. $cell = $table->addRow()->addCell();
  75. $cell->addText(htmlspecialchars('This cell contains nested table.'));
  76. $innerCell = $cell->addTable(array('align' => 'center'))->addRow()->addCell();
  77. $innerCell->addText(htmlspecialchars('Inside nested table'));
  78. // Save file
  79. echo write($phpWord, basename(__FILE__, '.php'), $writers);
  80. if (!CLI) {
  81. include_once 'Sample_Footer.php';
  82. }