No Description

Table.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\Word2007\Element;
  18. use PhpOffice\PhpWord\Element\Cell as CellElement;
  19. use PhpOffice\PhpWord\Element\Row as RowElement;
  20. use PhpOffice\PhpWord\Element\Table as TableElement;
  21. use PhpOffice\PhpWord\Shared\XMLWriter;
  22. use PhpOffice\PhpWord\Style\Cell as CellStyle;
  23. use PhpOffice\PhpWord\Style\Row as RowStyle;
  24. use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter;
  25. use PhpOffice\PhpWord\Writer\Word2007\Style\Row as RowStyleWriter;
  26. use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
  27. /**
  28. * Table element writer
  29. *
  30. * @since 0.10.0
  31. */
  32. class Table extends AbstractElement
  33. {
  34. /**
  35. * Write element.
  36. *
  37. * @return void
  38. */
  39. public function write()
  40. {
  41. $xmlWriter = $this->getXmlWriter();
  42. $element = $this->getElement();
  43. if (!$element instanceof TableElement) {
  44. return;
  45. }
  46. $rows = $element->getRows();
  47. $rowCount = count($rows);
  48. if ($rowCount > 0) {
  49. $xmlWriter->startElement('w:tbl');
  50. // Write columns
  51. $this->writeColumns($xmlWriter, $element);
  52. // Write style
  53. $styleWriter = new TableStyleWriter($xmlWriter, $element->getStyle());
  54. $styleWriter->setWidth($element->getWidth());
  55. $styleWriter->write();
  56. // Write rows
  57. for ($i = 0; $i < $rowCount; $i++) {
  58. $this->writeRow($xmlWriter, $rows[$i]);
  59. }
  60. $xmlWriter->endElement(); // w:tbl
  61. }
  62. }
  63. /**
  64. * Write column.
  65. *
  66. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  67. * @param \PhpOffice\PhpWord\Element\Table $element
  68. * @return void
  69. */
  70. private function writeColumns(XMLWriter $xmlWriter, TableElement $element)
  71. {
  72. $rows = $element->getRows();
  73. $rowCount = count($rows);
  74. $cellWidths = array();
  75. for ($i = 0; $i < $rowCount; $i++) {
  76. $row = $rows[$i];
  77. $cells = $row->getCells();
  78. if (count($cells) <= count($cellWidths)) {
  79. continue;
  80. }
  81. $cellWidths = array();
  82. foreach ($cells as $cell) {
  83. $cellWidths[] = $cell->getWidth();
  84. }
  85. }
  86. $xmlWriter->startElement('w:tblGrid');
  87. foreach ($cellWidths as $width) {
  88. $xmlWriter->startElement('w:gridCol');
  89. if ($width !== null) {
  90. $xmlWriter->writeAttribute('w:w', $width);
  91. $xmlWriter->writeAttribute('w:type', 'dxa');
  92. }
  93. $xmlWriter->endElement();
  94. }
  95. $xmlWriter->endElement(); // w:tblGrid
  96. }
  97. /**
  98. * Write row.
  99. *
  100. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  101. * @param \PhpOffice\PhpWord\Element\Row $row
  102. * @return void
  103. */
  104. private function writeRow(XMLWriter $xmlWriter, RowElement $row)
  105. {
  106. $xmlWriter->startElement('w:tr');
  107. // Write style
  108. $rowStyle = $row->getStyle();
  109. if ($rowStyle instanceof RowStyle) {
  110. $styleWriter = new RowStyleWriter($xmlWriter, $rowStyle);
  111. $styleWriter->setHeight($row->getHeight());
  112. $styleWriter->write();
  113. }
  114. // Write cells
  115. foreach ($row->getCells() as $cell) {
  116. $this->writeCell($xmlWriter, $cell);
  117. }
  118. $xmlWriter->endElement(); // w:tr
  119. }
  120. /**
  121. * Write cell.
  122. *
  123. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  124. * @param \PhpOffice\PhpWord\Element\Cell $cell
  125. * @return void
  126. */
  127. private function writeCell(XMLWriter $xmlWriter, CellElement $cell)
  128. {
  129. $xmlWriter->startElement('w:tc');
  130. // Write style
  131. $cellStyle = $cell->getStyle();
  132. if ($cellStyle instanceof CellStyle) {
  133. $styleWriter = new CellStyleWriter($xmlWriter, $cellStyle);
  134. $styleWriter->setWidth($cell->getWidth());
  135. $styleWriter->write();
  136. }
  137. // Write content
  138. $containerWriter = new Container($xmlWriter, $cell);
  139. $containerWriter->write();
  140. $xmlWriter->endElement(); // w:tc
  141. }
  142. }