PageRenderTime 23ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.3.5/Classes/PHPExcel/Writer/Excel2007/Workbook.php

#
PHP | 305 lines | 149 code | 38 blank | 118 comment | 33 complexity | 7b766c6178cb0a991bc43f7cbe07d57f MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 PHPExcel, Maarten Balliauw
  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_Excel2007
  23. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel */
  28. require_once 'PHPExcel.php';
  29. /** PHPExcel_Writer_Excel2007 */
  30. require_once 'PHPExcel/Writer/Excel2007.php';
  31. /** PHPExcel_Writer_Excel2007_WriterPart */
  32. require_once 'PHPExcel/Writer/Excel2007/WriterPart.php';
  33. /** PHPExcel_Cell */
  34. require_once 'PHPExcel/Cell.php';
  35. /**
  36. * PHPExcel_Writer_Excel2007_Workbook
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Writer_Excel2007
  40. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_WriterPart
  43. {
  44. /**
  45. * Write workbook to XML format
  46. *
  47. * @param PHPExcel $pPHPExcel
  48. * @return string XML Output
  49. * @throws Exception
  50. */
  51. public function writeWorkbook($pPHPExcel = null)
  52. {
  53. if ($pPHPExcel instanceof PHPExcel) {
  54. // Create XML writer
  55. $objWriter = new xmlWriter();
  56. $objWriter->openMemory();
  57. // XML header
  58. $objWriter->startDocument('1.0','UTF-8','yes');
  59. // workbook
  60. $objWriter->startElement('workbook');
  61. $objWriter->writeAttribute('xml:space', 'preserve');
  62. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  63. $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  64. // fileVersion
  65. $this->_writeFileVersion($objWriter);
  66. // workbookPr
  67. $this->_writeWorkbookPr($objWriter);
  68. // bookViews
  69. //$this->_writeBookViews($objWriter, $pPHPExcel);
  70. // workbookProtection
  71. $this->_writeWorkbookProtection($objWriter, $pPHPExcel);
  72. // sheets
  73. $this->_writeSheets($objWriter, $pPHPExcel);
  74. // definedNames
  75. $this->_writeDefinedNames($objWriter, $pPHPExcel);
  76. // calcPr
  77. $this->_writeCalcPr($objWriter);
  78. $objWriter->endElement();
  79. // Return
  80. return $objWriter->outputMemory(true);
  81. } else {
  82. throw new Exception("Invalid PHPExcel object passed.");
  83. }
  84. }
  85. /**
  86. * Write file version
  87. *
  88. * @param xmlWriter $objWriter XML Writer
  89. * @throws Exception
  90. */
  91. private function _writeFileVersion($objWriter = null)
  92. {
  93. if ($objWriter instanceof xmlWriter) {
  94. $objWriter->startElement('fileVersion');
  95. $objWriter->writeAttribute('appName', 'xl');
  96. $objWriter->writeAttribute('lastEdited', '4');
  97. $objWriter->writeAttribute('lowestEdited', '4');
  98. $objWriter->writeAttribute('rupBuild', '4505');
  99. $objWriter->endElement();
  100. } else {
  101. throw new Exception("Invalid parameters passed.");
  102. }
  103. }
  104. /**
  105. * Write WorkbookPr
  106. *
  107. * @param xmlWriter $objWriter XML Writer
  108. * @throws Exception
  109. */
  110. private function _writeWorkbookPr($objWriter = null)
  111. {
  112. if ($objWriter instanceof xmlWriter) {
  113. $objWriter->startElement('workbookPr');
  114. $objWriter->writeAttribute('codeName', 'ThisWorkbook');
  115. $objWriter->endElement();
  116. } else {
  117. throw new Exception("Invalid parameters passed.");
  118. }
  119. }
  120. /**
  121. * Write BookViews
  122. *
  123. * @param xmlWriter $objWriter XML Writer
  124. * @param PHPExcel $pPHPExcel
  125. * @throws Exception
  126. */
  127. private function _writeBookViews($objWriter = null, $pPHPExcel = null)
  128. {
  129. if ($objWriter instanceof xmlWriter && $pPHPExcel instanceof PHPExcel) {
  130. // bookViews
  131. $objWriter->startElement('bookViews');
  132. // workbookView
  133. $objWriter->startElement('workbookView');
  134. $objWriter->writeAttribute('activeTab', $pPHPExcel->getActiveSheetIndex());
  135. $objWriter->endElement();
  136. $objWriter->endElement();
  137. } else {
  138. throw new Exception("Invalid parameters passed.");
  139. }
  140. }
  141. /**
  142. * Write WorkbookProtection
  143. *
  144. * @param xmlWriter $objWriter XML Writer
  145. * @param PHPExcel $pPHPExcel
  146. * @throws Exception
  147. */
  148. private function _writeWorkbookProtection($objWriter = null, $pPHPExcel = null)
  149. {
  150. if ($objWriter instanceof xmlWriter && $pPHPExcel instanceof PHPExcel) {
  151. if ($pPHPExcel->getSecurity()->isSecurityEnabled()) {
  152. $objWriter->startElement('workbookProtection');
  153. $objWriter->writeAttribute('lockRevision', ($pPHPExcel->getSecurity()->getLockRevision() ? 'true' : 'false'));
  154. $objWriter->writeAttribute('lockStructure', ($pPHPExcel->getSecurity()->getLockStructure() ? 'true' : 'false'));
  155. $objWriter->writeAttribute('lockWindows', ($pPHPExcel->getSecurity()->getLockWindows() ? 'true' : 'false'));
  156. if ($pPHPExcel->getSecurity()->getRevisionsPassword() != '') {
  157. $objWriter->writeAttribute('revisionsPassword', $pPHPExcel->getSecurity()->getRevisionsPassword());
  158. }
  159. if ($pPHPExcel->getSecurity()->getWorkbookPassword() != '') {
  160. $objWriter->writeAttribute('workbookPassword', $pPHPExcel->getSecurity()->getWorkbookPassword());
  161. }
  162. $objWriter->endElement();
  163. }
  164. } else {
  165. throw new Exception("Invalid parameters passed.");
  166. }
  167. }
  168. /**
  169. * Write calcPr
  170. *
  171. * @param xmlWriter $objWriter XML Writer
  172. * @throws Exception
  173. */
  174. private function _writeCalcPr($objWriter = null)
  175. {
  176. if ($objWriter instanceof xmlWriter) {
  177. $objWriter->startElement('calcPr');
  178. $objWriter->writeAttribute('calcId', '122211');
  179. $objWriter->writeAttribute('calcMode', 'auto');
  180. $objWriter->writeAttribute('fullCalcOnLoad', 'true');
  181. $objWriter->endElement();
  182. } else {
  183. throw new Exception("Invalid parameters passed.");
  184. }
  185. }
  186. /**
  187. * Write sheets
  188. *
  189. * @param xmlWriter $objWriter XML Writer
  190. * @param PHPExcel $pPHPExcel
  191. * @throws Exception
  192. */
  193. private function _writeSheets($objWriter = null, $pPHPExcel = null)
  194. {
  195. if ($objWriter instanceof xmlWriter && $pPHPExcel instanceof PHPExcel) {
  196. // Write sheets
  197. $objWriter->startElement('sheets');
  198. for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++) {
  199. // sheet
  200. $this->_writeSheet(
  201. $objWriter,
  202. $pPHPExcel->getSheet($i)->getTitle(),
  203. ($i + 1),
  204. ($i + 1 + 3)
  205. );
  206. }
  207. $objWriter->endElement();
  208. } else {
  209. throw new Exception("Invalid parameters passed.");
  210. }
  211. }
  212. /**
  213. * Write sheet
  214. *
  215. * @param xmlWriter $objWriter XML Writer
  216. * @param string $pSheetname Sheet name
  217. * @param int $pSheetId Sheet id
  218. * @param int $pRelId Relationship ID
  219. * @throws Exception
  220. */
  221. private function _writeSheet($objWriter = null, $pSheetname = '', $pSheetId = 1, $pRelId = 1)
  222. {
  223. if ($objWriter instanceof xmlWriter && $pSheetname != '') {
  224. // Write sheet
  225. $objWriter->startElement('sheet');
  226. $objWriter->writeAttribute('name', $pSheetname);
  227. $objWriter->writeAttribute('sheetId', $pSheetId);
  228. $objWriter->writeAttribute('r:id', 'rId' . $pRelId);
  229. $objWriter->endElement();
  230. } else {
  231. throw new Exception("Invalid parameters passed.");
  232. }
  233. }
  234. /**
  235. * Write Defined Names
  236. *
  237. * @param xmlWriter $objWriter XML Writer
  238. * @param PHPExcel $pPHPExcel
  239. * @throws Exception
  240. */
  241. private function _writeDefinedNames($objWriter = null, $pPHPExcel = null)
  242. {
  243. if ($objWriter instanceof xmlWriter && $pPHPExcel instanceof PHPExcel) {
  244. // Write defined names
  245. $objWriter->startElement('definedNames');
  246. for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++) {
  247. // definedName for autoFilter
  248. if ($pPHPExcel->getSheet($i)->getAutoFilter() != '') {
  249. $objWriter->startElement('definedName');
  250. $objWriter->writeAttribute('name', '_xlnm._FilterDatabase');
  251. $objWriter->writeAttribute('localSheetId', $i);
  252. $objWriter->writeAttribute('hidden', '1');
  253. // Create absolute coordinate and write as raw text
  254. $range = PHPExcel_Cell::splitRange($pPHPExcel->getSheet($i)->getAutoFilter());
  255. $range[0] = PHPExcel_Cell::absoluteCoordinate($range[0]);
  256. $range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]);
  257. $range = implode(':', $range);
  258. $objWriter->writeRaw('\'' . $pPHPExcel->getSheet($i)->getTitle() . '\'!' . $range);
  259. $objWriter->endElement();
  260. }
  261. }
  262. $objWriter->endElement();
  263. } else {
  264. throw new Exception("Invalid parameters passed.");
  265. }
  266. }
  267. }