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

# · PHP · 440 lines · 203 code · 65 blank · 172 comment · 24 complexity · 9e7cf222d830e342a0e6a507216b15b3 MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2008 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 - 2008 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. /** 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 - 2008 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. // workbookProtection
  74. $this->_writeWorkbookProtection($objWriter, $pPHPExcel);
  75. // bookViews
  76. if ($this->getParentWriter()->getOffice2003Compatibility() === false) {
  77. $this->_writeBookViews($objWriter, $pPHPExcel);
  78. }
  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. $sheetCount = $pPHPExcel->getSheetCount();
  190. for ($i = 0; $i < $sheetCount; ++$i) {
  191. // sheet
  192. $this->_writeSheet(
  193. $objWriter,
  194. $pPHPExcel->getSheet($i)->getTitle(),
  195. ($i + 1),
  196. ($i + 1 + 3)
  197. );
  198. }
  199. $objWriter->endElement();
  200. }
  201. /**
  202. * Write sheet
  203. *
  204. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  205. * @param string $pSheetname Sheet name
  206. * @param int $pSheetId Sheet id
  207. * @param int $pRelId Relationship ID
  208. * @throws Exception
  209. */
  210. private function _writeSheet(PHPExcel_Shared_XMLWriter $objWriter = null, $pSheetname = '', $pSheetId = 1, $pRelId = 1)
  211. {
  212. if ($pSheetname != '') {
  213. // Write sheet
  214. $objWriter->startElement('sheet');
  215. $objWriter->writeAttribute('name', $pSheetname);
  216. $objWriter->writeAttribute('sheetId', $pSheetId);
  217. $objWriter->writeAttribute('r:id', 'rId' . $pRelId);
  218. $objWriter->endElement();
  219. } else {
  220. throw new Exception("Invalid parameters passed.");
  221. }
  222. }
  223. /**
  224. * Write Defined Names
  225. *
  226. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  227. * @param PHPExcel $pPHPExcel
  228. * @throws Exception
  229. */
  230. private function _writeDefinedNames(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  231. {
  232. // Write defined names
  233. $objWriter->startElement('definedNames');
  234. // Named ranges
  235. if (count($pPHPExcel->getNamedRanges()) > 0) {
  236. // Named ranges
  237. $this->_writeNamedRanges($objWriter, $pPHPExcel);
  238. }
  239. // Other defined names
  240. $sheetCount = $pPHPExcel->getSheetCount();
  241. for ($i = 0; $i < $sheetCount; ++$i) {
  242. // definedName for autoFilter
  243. $this->_writeDefinedNameForAutofilter($objWriter, $pPHPExcel->getSheet($i), $i);
  244. // definedName for Print_Titles
  245. $this->_writeDefinedNameForPrintTitles($objWriter, $pPHPExcel->getSheet($i), $i);
  246. // definedName for Print_Area
  247. $this->_writeDefinedNameForPrintArea($objWriter, $pPHPExcel->getSheet($i), $i);
  248. }
  249. $objWriter->endElement();
  250. }
  251. /**
  252. * Write named ranges
  253. *
  254. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  255. * @param PHPExcel $pPHPExcel
  256. * @throws Exception
  257. */
  258. private function _writeNamedRanges(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel)
  259. {
  260. // Loop named ranges
  261. $namedRanges = $pPHPExcel->getNamedRanges();
  262. foreach ($namedRanges as $namedRange) {
  263. $this->_writeDefinedNameForNamedRange($objWriter, $namedRange);
  264. }
  265. }
  266. /**
  267. * Write Defined Name for autoFilter
  268. *
  269. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  270. * @param PHPExcel_NamedRange $pNamedRange
  271. * @throws Exception
  272. */
  273. private function _writeDefinedNameForNamedRange(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_NamedRange $pNamedRange)
  274. {
  275. // definedName for named range
  276. $objWriter->startElement('definedName');
  277. $objWriter->writeAttribute('name', $pNamedRange->getName());
  278. if ($pNamedRange->getLocalOnly()) {
  279. $objWriter->writeAttribute('localSheetId', $pNamedRange->getWorksheet()->getParent()->getIndex($pNamedRange->getWorksheet()));
  280. }
  281. // Create absolute coordinate and write as raw text
  282. $range = PHPExcel_Cell::splitRange($pNamedRange->getRange());
  283. $range[0] = PHPExcel_Cell::absoluteCoordinate($range[0]);
  284. if (isset($range[1])) {
  285. $range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]);
  286. }
  287. $range = implode(':', $range);
  288. $objWriter->writeRaw('\'' . $pNamedRange->getWorksheet()->getTitle() . '\'!' . $range);
  289. $objWriter->endElement();
  290. }
  291. /**
  292. * Write Defined Name for autoFilter
  293. *
  294. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  295. * @param PHPExcel_Worksheet $pSheet
  296. * @param int $pSheetId
  297. * @throws Exception
  298. */
  299. private function _writeDefinedNameForAutofilter(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  300. {
  301. // definedName for autoFilter
  302. if ($pSheet->getAutoFilter() != '') {
  303. $objWriter->startElement('definedName');
  304. $objWriter->writeAttribute('name', '_xlnm._FilterDatabase');
  305. $objWriter->writeAttribute('localSheetId', $pSheetId);
  306. $objWriter->writeAttribute('hidden', '1');
  307. // Create absolute coordinate and write as raw text
  308. $range = PHPExcel_Cell::splitRange($pSheet->getAutoFilter());
  309. $range[0] = PHPExcel_Cell::absoluteCoordinate($range[0]);
  310. $range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]);
  311. $range = implode(':', $range);
  312. $objWriter->writeRaw('\'' . $pSheet->getTitle() . '\'!' . $range);
  313. $objWriter->endElement();
  314. }
  315. }
  316. /**
  317. * Write Defined Name for PrintTitles
  318. *
  319. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  320. * @param PHPExcel_Worksheet $pSheet
  321. * @param int $pSheetId
  322. * @throws Exception
  323. */
  324. private function _writeDefinedNameForPrintTitles(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  325. {
  326. // definedName for PrintTitles
  327. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet() || $pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
  328. $objWriter->startElement('definedName');
  329. $objWriter->writeAttribute('name', '_xlnm.Print_Titles');
  330. $objWriter->writeAttribute('localSheetId', $pSheetId);
  331. // Setting string
  332. $settingString = '';
  333. // Columns to repeat
  334. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
  335. $repeat = $pSheet->getPageSetup()->getColumnsToRepeatAtLeft();
  336. $settingString = '\'' . $pSheet->getTitle() . '\'!$' . $repeat[0] . ':$' . $repeat[1];
  337. }
  338. // Rows to repeat
  339. if ($pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
  340. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
  341. $settingString .= ',';
  342. }
  343. $repeat = $pSheet->getPageSetup()->getRowsToRepeatAtTop();
  344. $settingString = '\'' . $pSheet->getTitle() . '\'!$' . $repeat[0] . ':$' . $repeat[1];
  345. }
  346. $objWriter->writeRaw($settingString);
  347. $objWriter->endElement();
  348. }
  349. }
  350. /**
  351. * Write Defined Name for PrintTitles
  352. *
  353. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  354. * @param PHPExcel_Worksheet $pSheet
  355. * @param int $pSheetId
  356. * @throws Exception
  357. */
  358. private function _writeDefinedNameForPrintArea(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  359. {
  360. // definedName for PrintArea
  361. if ($pSheet->getPageSetup()->isPrintAreaSet()) {
  362. $objWriter->startElement('definedName');
  363. $objWriter->writeAttribute('name', '_xlnm.Print_Area');
  364. $objWriter->writeAttribute('localSheetId', $pSheetId);
  365. // Setting string
  366. $settingString = '';
  367. // Print area
  368. $printArea = PHPExcel_Cell::splitRange($pSheet->getPageSetup()->getPrintArea());
  369. $printArea[0] = PHPExcel_Cell::absoluteCoordinate($printArea[0]);
  370. $printArea[1] = PHPExcel_Cell::absoluteCoordinate($printArea[1]);
  371. $objWriter->writeRaw('\'' . $pSheet->getTitle() . '\'!' . implode(':', $printArea));
  372. $objWriter->endElement();
  373. }
  374. }
  375. }