PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 354 lines | 160 code | 54 blank | 140 comment | 20 complexity | ce6ef2357c6e06b5df2906f4903f89a5 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
  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. /** PHPExcel_Shared_XMLWriter */
  36. require_once 'PHPExcel/Shared/XMLWriter.php';
  37. /**
  38. * PHPExcel_Writer_Excel2007_Workbook
  39. *
  40. * @category PHPExcel
  41. * @package PHPExcel_Writer_Excel2007
  42. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  43. */
  44. class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_WriterPart
  45. {
  46. /**
  47. * Write workbook to XML format
  48. *
  49. * @param PHPExcel $pPHPExcel
  50. * @return string XML Output
  51. * @throws Exception
  52. */
  53. public function writeWorkbook(PHPExcel $pPHPExcel = null)
  54. {
  55. // Create XML writer
  56. $objWriter = null;
  57. if ($this->getParentWriter()->getUseDiskCaching()) {
  58. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  59. } else {
  60. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  61. }
  62. // XML header
  63. $objWriter->startDocument('1.0','UTF-8','yes');
  64. // workbook
  65. $objWriter->startElement('workbook');
  66. $objWriter->writeAttribute('xml:space', 'preserve');
  67. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  68. $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  69. // fileVersion
  70. $this->_writeFileVersion($objWriter);
  71. // workbookPr
  72. $this->_writeWorkbookPr($objWriter);
  73. // bookViews
  74. if ($this->getParentWriter()->getOffice2003Compatibility() === false) {
  75. $this->_writeBookViews($objWriter, $pPHPExcel);
  76. }
  77. // workbookProtection
  78. $this->_writeWorkbookProtection($objWriter, $pPHPExcel);
  79. // sheets
  80. $this->_writeSheets($objWriter, $pPHPExcel);
  81. // definedNames
  82. $this->_writeDefinedNames($objWriter, $pPHPExcel);
  83. // calcPr
  84. $this->_writeCalcPr($objWriter);
  85. $objWriter->endElement();
  86. // Return
  87. return $objWriter->getData();
  88. }
  89. /**
  90. * Write file version
  91. *
  92. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  93. * @throws Exception
  94. */
  95. private function _writeFileVersion(PHPExcel_Shared_XMLWriter $objWriter = null)
  96. {
  97. $objWriter->startElement('fileVersion');
  98. $objWriter->writeAttribute('appName', 'xl');
  99. $objWriter->writeAttribute('lastEdited', '4');
  100. $objWriter->writeAttribute('lowestEdited', '4');
  101. $objWriter->writeAttribute('rupBuild', '4505');
  102. $objWriter->endElement();
  103. }
  104. /**
  105. * Write WorkbookPr
  106. *
  107. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  108. * @throws Exception
  109. */
  110. private function _writeWorkbookPr(PHPExcel_Shared_XMLWriter $objWriter = null)
  111. {
  112. $objWriter->startElement('workbookPr');
  113. $objWriter->writeAttribute('codeName', 'ThisWorkbook');
  114. $objWriter->endElement();
  115. }
  116. /**
  117. * Write BookViews
  118. *
  119. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  120. * @param PHPExcel $pPHPExcel
  121. * @throws Exception
  122. */
  123. private function _writeBookViews(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  124. {
  125. // bookViews
  126. $objWriter->startElement('bookViews');
  127. // workbookView
  128. $objWriter->startElement('workbookView');
  129. $objWriter->writeAttribute('activeTab', $pPHPExcel->getActiveSheetIndex());
  130. $objWriter->writeAttribute('autoFilterDateGrouping', '1');
  131. $objWriter->writeAttribute('firstSheet', '0');
  132. $objWriter->writeAttribute('minimized', '0');
  133. $objWriter->writeAttribute('showHorizontalScroll', '1');
  134. $objWriter->writeAttribute('showSheetTabs', '1');
  135. $objWriter->writeAttribute('showVerticalScroll', '1');
  136. $objWriter->writeAttribute('tabRatio', '600');
  137. $objWriter->writeAttribute('visibility', 'visible');
  138. $objWriter->endElement();
  139. $objWriter->endElement();
  140. }
  141. /**
  142. * Write WorkbookProtection
  143. *
  144. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  145. * @param PHPExcel $pPHPExcel
  146. * @throws Exception
  147. */
  148. private function _writeWorkbookProtection(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  149. {
  150. if ($pPHPExcel->getSecurity()->isSecurityEnabled()) {
  151. $objWriter->startElement('workbookProtection');
  152. $objWriter->writeAttribute('lockRevision', ($pPHPExcel->getSecurity()->getLockRevision() ? 'true' : 'false'));
  153. $objWriter->writeAttribute('lockStructure', ($pPHPExcel->getSecurity()->getLockStructure() ? 'true' : 'false'));
  154. $objWriter->writeAttribute('lockWindows', ($pPHPExcel->getSecurity()->getLockWindows() ? 'true' : 'false'));
  155. if ($pPHPExcel->getSecurity()->getRevisionsPassword() != '') {
  156. $objWriter->writeAttribute('revisionsPassword', $pPHPExcel->getSecurity()->getRevisionsPassword());
  157. }
  158. if ($pPHPExcel->getSecurity()->getWorkbookPassword() != '') {
  159. $objWriter->writeAttribute('workbookPassword', $pPHPExcel->getSecurity()->getWorkbookPassword());
  160. }
  161. $objWriter->endElement();
  162. }
  163. }
  164. /**
  165. * Write calcPr
  166. *
  167. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  168. * @throws Exception
  169. */
  170. private function _writeCalcPr(PHPExcel_Shared_XMLWriter $objWriter = null)
  171. {
  172. $objWriter->startElement('calcPr');
  173. $objWriter->writeAttribute('calcId', '124519');
  174. $objWriter->writeAttribute('calcMode', 'auto');
  175. $objWriter->writeAttribute('fullCalcOnLoad', '1');
  176. $objWriter->endElement();
  177. }
  178. /**
  179. * Write sheets
  180. *
  181. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  182. * @param PHPExcel $pPHPExcel
  183. * @throws Exception
  184. */
  185. private function _writeSheets(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  186. {
  187. // Write sheets
  188. $objWriter->startElement('sheets');
  189. for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++) {
  190. // sheet
  191. $this->_writeSheet(
  192. $objWriter,
  193. $pPHPExcel->getSheet($i)->getTitle(),
  194. ($i + 1),
  195. ($i + 1 + 3)
  196. );
  197. }
  198. $objWriter->endElement();
  199. }
  200. /**
  201. * Write sheet
  202. *
  203. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  204. * @param string $pSheetname Sheet name
  205. * @param int $pSheetId Sheet id
  206. * @param int $pRelId Relationship ID
  207. * @throws Exception
  208. */
  209. private function _writeSheet(PHPExcel_Shared_XMLWriter $objWriter = null, $pSheetname = '', $pSheetId = 1, $pRelId = 1)
  210. {
  211. if ($pSheetname != '') {
  212. // Write sheet
  213. $objWriter->startElement('sheet');
  214. $objWriter->writeAttribute('name', $pSheetname);
  215. $objWriter->writeAttribute('sheetId', $pSheetId);
  216. $objWriter->writeAttribute('r:id', 'rId' . $pRelId);
  217. $objWriter->endElement();
  218. } else {
  219. throw new Exception("Invalid parameters passed.");
  220. }
  221. }
  222. /**
  223. * Write Defined Names
  224. *
  225. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  226. * @param PHPExcel $pPHPExcel
  227. * @throws Exception
  228. */
  229. private function _writeDefinedNames(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  230. {
  231. // Write defined names
  232. $objWriter->startElement('definedNames');
  233. for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++) {
  234. // definedName for autoFilter
  235. $this->_writeDefinedNameForAutofilter($objWriter, $pPHPExcel->getSheet($i), $i);
  236. // definedName for Print_Titles
  237. $this->_writeDefinedNameForPrintTitles($objWriter, $pPHPExcel->getSheet($i), $i);
  238. }
  239. $objWriter->endElement();
  240. }
  241. /**
  242. * Write Defined Name for autoFilter
  243. *
  244. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  245. * @param PHPExcel_Worksheet $pSheet
  246. * @param int $pSheetId
  247. * @throws Exception
  248. */
  249. private function _writeDefinedNameForAutofilter(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  250. {
  251. // definedName for autoFilter
  252. if ($pSheet->getAutoFilter() != '') {
  253. $objWriter->startElement('definedName');
  254. $objWriter->writeAttribute('name', '_xlnm._FilterDatabase');
  255. $objWriter->writeAttribute('localSheetId', $pSheetId);
  256. $objWriter->writeAttribute('hidden', '1');
  257. // Create absolute coordinate and write as raw text
  258. $range = PHPExcel_Cell::splitRange($pSheet->getAutoFilter());
  259. $range[0] = PHPExcel_Cell::absoluteCoordinate($range[0]);
  260. $range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]);
  261. $range = implode(':', $range);
  262. $objWriter->writeRaw('\'' . $pSheet->getTitle() . '\'!' . $range);
  263. $objWriter->endElement();
  264. }
  265. }
  266. /**
  267. * Write Defined Name for PrintTitles
  268. *
  269. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  270. * @param PHPExcel_Worksheet $pSheet
  271. * @param int $pSheetId
  272. * @throws Exception
  273. */
  274. private function _writeDefinedNameForPrintTitles(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  275. {
  276. // definedName for PrintTitles
  277. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet() || $pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
  278. $objWriter->startElement('definedName');
  279. $objWriter->writeAttribute('name', '_xlnm.Print_Titles');
  280. $objWriter->writeAttribute('localSheetId', $pSheetId);
  281. // Setting string
  282. $settingString = '';
  283. // Columns to repeat
  284. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
  285. $repeat = $pSheet->getPageSetup()->getColumnsToRepeatAtLeft();
  286. $settingString = $pSheet->getTitle() . '!$' . $repeat[0] . ':$' . $repeat[1];
  287. }
  288. // Rows to repeat
  289. if ($pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
  290. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
  291. $settingString .= ',';
  292. }
  293. $repeat = $pSheet->getPageSetup()->getRowsToRepeatAtTop();
  294. $settingString = $pSheet->getTitle() . '!$' . $repeat[0] . ':$' . $repeat[1];
  295. }
  296. $objWriter->writeRaw($settingString);
  297. $objWriter->endElement();
  298. }
  299. }
  300. }