No Description

Content.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer_OpenDocument
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Writer_OpenDocument_Content
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_OpenDocument
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. * @author Alexander Pervakov <frost-nzcr4@jagmort.com>
  34. */
  35. class PHPExcel_Writer_OpenDocument_Content extends PHPExcel_Writer_OpenDocument_WriterPart
  36. {
  37. const NUMBER_COLS_REPEATED_MAX = 1024;
  38. const NUMBER_ROWS_REPEATED_MAX = 1048576;
  39. /**
  40. * Write content.xml to XML format
  41. *
  42. * @param PHPExcel $pPHPExcel
  43. * @return string XML Output
  44. * @throws PHPExcel_Writer_Exception
  45. */
  46. public function write(PHPExcel $pPHPExcel = null)
  47. {
  48. if (!$pPHPExcel) {
  49. $pPHPExcel = $this->getParentWriter()->getPHPExcel(); /* @var $pPHPExcel PHPExcel */
  50. }
  51. $objWriter = null;
  52. if ($this->getParentWriter()->getUseDiskCaching()) {
  53. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  54. } else {
  55. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  56. }
  57. // XML header
  58. $objWriter->startDocument('1.0', 'UTF-8');
  59. // Content
  60. $objWriter->startElement('office:document-content');
  61. $objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
  62. $objWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0');
  63. $objWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0');
  64. $objWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0');
  65. $objWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0');
  66. $objWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
  67. $objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
  68. $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
  69. $objWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
  70. $objWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0');
  71. $objWriter->writeAttribute('xmlns:presentation', 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0');
  72. $objWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0');
  73. $objWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0');
  74. $objWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0');
  75. $objWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML');
  76. $objWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0');
  77. $objWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0');
  78. $objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
  79. $objWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer');
  80. $objWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc');
  81. $objWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events');
  82. $objWriter->writeAttribute('xmlns:xforms', 'http://www.w3.org/2002/xforms');
  83. $objWriter->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
  84. $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  85. $objWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report');
  86. $objWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2');
  87. $objWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
  88. $objWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
  89. $objWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table');
  90. $objWriter->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0');
  91. $objWriter->writeAttribute('xmlns:formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0');
  92. $objWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/');
  93. $objWriter->writeAttribute('office:version', '1.2');
  94. $objWriter->writeElement('office:scripts');
  95. $objWriter->writeElement('office:font-face-decls');
  96. $objWriter->writeElement('office:automatic-styles');
  97. $objWriter->startElement('office:body');
  98. $objWriter->startElement('office:spreadsheet');
  99. $objWriter->writeElement('table:calculation-settings');
  100. $this->_writeSheets($objWriter);
  101. $objWriter->writeElement('table:named-expressions');
  102. $objWriter->endElement();
  103. $objWriter->endElement();
  104. $objWriter->endElement();
  105. return $objWriter->getData();
  106. }
  107. /**
  108. * Write sheets
  109. *
  110. * @param PHPExcel_Shared_XMLWriter $objWriter
  111. */
  112. private function _writeSheets(PHPExcel_Shared_XMLWriter $objWriter)
  113. {
  114. $pPHPExcel = $this->getParentWriter()->getPHPExcel(); /* @var $pPHPExcel PHPExcel */
  115. $sheet_count = $pPHPExcel->getSheetCount();
  116. for ($i = 0; $i < $sheet_count; $i++) {
  117. //$this->getWriterPart('Worksheet')->writeWorksheet());
  118. $objWriter->startElement('table:table');
  119. $objWriter->writeAttribute('table:name', $pPHPExcel->getSheet($i)->getTitle());
  120. $objWriter->writeElement('office:forms');
  121. $objWriter->startElement('table:table-column');
  122. $objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
  123. $objWriter->endElement();
  124. $this->_writeRows($objWriter, $pPHPExcel->getSheet($i));
  125. $objWriter->endElement();
  126. }
  127. }
  128. /**
  129. * Write rows of the specified sheet
  130. *
  131. * @param PHPExcel_Shared_XMLWriter $objWriter
  132. * @param PHPExcel_Worksheet $sheet
  133. */
  134. private function _writeRows(PHPExcel_Shared_XMLWriter $objWriter, PHPExcel_Worksheet $sheet)
  135. {
  136. $number_rows_repeated = self::NUMBER_ROWS_REPEATED_MAX;
  137. $span_row = 0;
  138. $rows = $sheet->getRowIterator();
  139. while ($rows->valid()) {
  140. $number_rows_repeated--;
  141. $row = $rows->current();
  142. if ($row->getCellIterator()->valid()) {
  143. if ($span_row) {
  144. $objWriter->startElement('table:table-row');
  145. if ($span_row > 1) {
  146. $objWriter->writeAttribute('table:number-rows-repeated', $span_row);
  147. }
  148. $objWriter->startElement('table:table-cell');
  149. $objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
  150. $objWriter->endElement();
  151. $objWriter->endElement();
  152. $span_row = 0;
  153. }
  154. $objWriter->startElement('table:table-row');
  155. $this->_writeCells($objWriter, $row);
  156. $objWriter->endElement();
  157. } else {
  158. $span_row++;
  159. }
  160. $rows->next();
  161. }
  162. }
  163. /**
  164. * Write cells of the specified row
  165. *
  166. * @param PHPExcel_Shared_XMLWriter $objWriter
  167. * @param PHPExcel_Worksheet_Row $row
  168. * @throws PHPExcel_Writer_Exception
  169. */
  170. private function _writeCells(PHPExcel_Shared_XMLWriter $objWriter, PHPExcel_Worksheet_Row $row)
  171. {
  172. $number_cols_repeated = self::NUMBER_COLS_REPEATED_MAX;
  173. $prev_column = -1;
  174. $cells = $row->getCellIterator();
  175. while ($cells->valid()) {
  176. $cell = $cells->current();
  177. $column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1;
  178. $this->_writeCellSpan($objWriter, $column, $prev_column);
  179. $objWriter->startElement('table:table-cell');
  180. switch ($cell->getDataType()) {
  181. case PHPExcel_Cell_DataType::TYPE_BOOL:
  182. $objWriter->writeAttribute('office:value-type', 'boolean');
  183. $objWriter->writeAttribute('office:value', $cell->getValue());
  184. $objWriter->writeElement('text:p', $cell->getValue());
  185. break;
  186. case PHPExcel_Cell_DataType::TYPE_ERROR:
  187. throw new PHPExcel_Writer_Exception('Writing of error not implemented yet.');
  188. break;
  189. case PHPExcel_Cell_DataType::TYPE_FORMULA:
  190. try {
  191. $formula_value = $cell->getCalculatedValue();
  192. } catch (Exception $e) {
  193. $formula_value = $cell->getValue();
  194. }
  195. $objWriter->writeAttribute('table:formula', 'of:' . $cell->getValue());
  196. if (is_numeric($formula_value)) {
  197. $objWriter->writeAttribute('office:value-type', 'float');
  198. } else {
  199. $objWriter->writeAttribute('office:value-type', 'string');
  200. }
  201. $objWriter->writeAttribute('office:value', $formula_value);
  202. $objWriter->writeElement('text:p', $formula_value);
  203. break;
  204. case PHPExcel_Cell_DataType::TYPE_INLINE:
  205. throw new PHPExcel_Writer_Exception('Writing of inline not implemented yet.');
  206. break;
  207. case PHPExcel_Cell_DataType::TYPE_NUMERIC:
  208. $objWriter->writeAttribute('office:value-type', 'float');
  209. $objWriter->writeAttribute('office:value', $cell->getValue());
  210. $objWriter->writeElement('text:p', $cell->getValue());
  211. break;
  212. case PHPExcel_Cell_DataType::TYPE_STRING:
  213. $objWriter->writeAttribute('office:value-type', 'string');
  214. $objWriter->writeElement('text:p', $cell->getValue());
  215. break;
  216. }
  217. PHPExcel_Writer_OpenDocument_Cell_Comment::write($objWriter, $cell);
  218. $objWriter->endElement();
  219. $prev_column = $column;
  220. $cells->next();
  221. }
  222. $number_cols_repeated = $number_cols_repeated - $prev_column - 1;
  223. if ($number_cols_repeated > 0) {
  224. if ($number_cols_repeated > 1) {
  225. $objWriter->startElement('table:table-cell');
  226. $objWriter->writeAttribute('table:number-columns-repeated', $number_cols_repeated);
  227. $objWriter->endElement();
  228. } else {
  229. $objWriter->writeElement('table:table-cell');
  230. }
  231. }
  232. }
  233. /**
  234. * Write span
  235. *
  236. * @param PHPExcel_Shared_XMLWriter $objWriter
  237. * @param integer $curColumn
  238. * @param integer $prevColumn
  239. */
  240. private function _writeCellSpan(PHPExcel_Shared_XMLWriter $objWriter, $curColumn, $prevColumn)
  241. {
  242. $diff = $curColumn - $prevColumn - 1;
  243. if (1 === $diff) {
  244. $objWriter->writeElement('table:table-cell');
  245. } elseif ($diff > 1) {
  246. $objWriter->startElement('table:table-cell');
  247. $objWriter->writeAttribute('table:number-columns-repeated', $diff);
  248. $objWriter->endElement();
  249. }
  250. }
  251. }