Workbook.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Writer_Excel2007_Workbook
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_WriterPart
  35. {
  36. /**
  37. * Write workbook to XML format
  38. *
  39. * @param PHPExcel $pPHPExcel
  40. * @param boolean $recalcRequired Indicate whether formulas should be recalculated before writing
  41. * @return string XML Output
  42. * @throws PHPExcel_Writer_Exception
  43. */
  44. public function writeWorkbook(PHPExcel $pPHPExcel = null, $recalcRequired = FALSE)
  45. {
  46. // Create XML writer
  47. $objWriter = null;
  48. if ($this->getParentWriter()->getUseDiskCaching()) {
  49. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  50. } else {
  51. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  52. }
  53. // XML header
  54. $objWriter->startDocument('1.0','UTF-8','yes');
  55. // workbook
  56. $objWriter->startElement('workbook');
  57. $objWriter->writeAttribute('xml:space', 'preserve');
  58. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  59. $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  60. // fileVersion
  61. $this->_writeFileVersion($objWriter);
  62. // workbookPr
  63. $this->_writeWorkbookPr($objWriter);
  64. // workbookProtection
  65. $this->_writeWorkbookProtection($objWriter, $pPHPExcel);
  66. // bookViews
  67. if ($this->getParentWriter()->getOffice2003Compatibility() === false) {
  68. $this->_writeBookViews($objWriter, $pPHPExcel);
  69. }
  70. // sheets
  71. $this->_writeSheets($objWriter, $pPHPExcel);
  72. // definedNames
  73. $this->_writeDefinedNames($objWriter, $pPHPExcel);
  74. // calcPr
  75. $this->_writeCalcPr($objWriter,$recalcRequired);
  76. $objWriter->endElement();
  77. // Return
  78. return $objWriter->getData();
  79. }
  80. /**
  81. * Write file version
  82. *
  83. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  84. * @throws PHPExcel_Writer_Exception
  85. */
  86. private function _writeFileVersion(PHPExcel_Shared_XMLWriter $objWriter = null)
  87. {
  88. $objWriter->startElement('fileVersion');
  89. $objWriter->writeAttribute('appName', 'xl');
  90. $objWriter->writeAttribute('lastEdited', '4');
  91. $objWriter->writeAttribute('lowestEdited', '4');
  92. $objWriter->writeAttribute('rupBuild', '4505');
  93. $objWriter->endElement();
  94. }
  95. /**
  96. * Write WorkbookPr
  97. *
  98. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  99. * @throws PHPExcel_Writer_Exception
  100. */
  101. private function _writeWorkbookPr(PHPExcel_Shared_XMLWriter $objWriter = null)
  102. {
  103. $objWriter->startElement('workbookPr');
  104. if (PHPExcel_Shared_Date::getExcelCalendar() == PHPExcel_Shared_Date::CALENDAR_MAC_1904) {
  105. $objWriter->writeAttribute('date1904', '1');
  106. }
  107. $objWriter->writeAttribute('codeName', 'ThisWorkbook');
  108. $objWriter->endElement();
  109. }
  110. /**
  111. * Write BookViews
  112. *
  113. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  114. * @param PHPExcel $pPHPExcel
  115. * @throws PHPExcel_Writer_Exception
  116. */
  117. private function _writeBookViews(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  118. {
  119. // bookViews
  120. $objWriter->startElement('bookViews');
  121. // workbookView
  122. $objWriter->startElement('workbookView');
  123. $objWriter->writeAttribute('activeTab', $pPHPExcel->getActiveSheetIndex());
  124. $objWriter->writeAttribute('autoFilterDateGrouping', '1');
  125. $objWriter->writeAttribute('firstSheet', '0');
  126. $objWriter->writeAttribute('minimized', '0');
  127. $objWriter->writeAttribute('showHorizontalScroll', '1');
  128. $objWriter->writeAttribute('showSheetTabs', '1');
  129. $objWriter->writeAttribute('showVerticalScroll', '1');
  130. $objWriter->writeAttribute('tabRatio', '600');
  131. $objWriter->writeAttribute('visibility', 'visible');
  132. $objWriter->endElement();
  133. $objWriter->endElement();
  134. }
  135. /**
  136. * Write WorkbookProtection
  137. *
  138. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  139. * @param PHPExcel $pPHPExcel
  140. * @throws PHPExcel_Writer_Exception
  141. */
  142. private function _writeWorkbookProtection(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  143. {
  144. if ($pPHPExcel->getSecurity()->isSecurityEnabled()) {
  145. $objWriter->startElement('workbookProtection');
  146. $objWriter->writeAttribute('lockRevision', ($pPHPExcel->getSecurity()->getLockRevision() ? 'true' : 'false'));
  147. $objWriter->writeAttribute('lockStructure', ($pPHPExcel->getSecurity()->getLockStructure() ? 'true' : 'false'));
  148. $objWriter->writeAttribute('lockWindows', ($pPHPExcel->getSecurity()->getLockWindows() ? 'true' : 'false'));
  149. if ($pPHPExcel->getSecurity()->getRevisionsPassword() != '') {
  150. $objWriter->writeAttribute('revisionsPassword', $pPHPExcel->getSecurity()->getRevisionsPassword());
  151. }
  152. if ($pPHPExcel->getSecurity()->getWorkbookPassword() != '') {
  153. $objWriter->writeAttribute('workbookPassword', $pPHPExcel->getSecurity()->getWorkbookPassword());
  154. }
  155. $objWriter->endElement();
  156. }
  157. }
  158. /**
  159. * Write calcPr
  160. *
  161. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  162. * @param boolean $recalcRequired Indicate whether formulas should be recalculated before writing
  163. * @throws PHPExcel_Writer_Exception
  164. */
  165. private function _writeCalcPr(PHPExcel_Shared_XMLWriter $objWriter = null, $recalcRequired = TRUE)
  166. {
  167. $objWriter->startElement('calcPr');
  168. $objWriter->writeAttribute('calcId', '124519');
  169. $objWriter->writeAttribute('calcMode', 'auto');
  170. // fullCalcOnLoad isn't needed if we've recalculating for the save
  171. $objWriter->writeAttribute('fullCalcOnLoad', ($recalcRequired) ? '0' : '1');
  172. $objWriter->endElement();
  173. }
  174. /**
  175. * Write sheets
  176. *
  177. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  178. * @param PHPExcel $pPHPExcel
  179. * @throws PHPExcel_Writer_Exception
  180. */
  181. private function _writeSheets(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  182. {
  183. // Write sheets
  184. $objWriter->startElement('sheets');
  185. $sheetCount = $pPHPExcel->getSheetCount();
  186. for ($i = 0; $i < $sheetCount; ++$i) {
  187. // sheet
  188. $this->_writeSheet(
  189. $objWriter,
  190. $pPHPExcel->getSheet($i)->getTitle(),
  191. ($i + 1),
  192. ($i + 1 + 3),
  193. $pPHPExcel->getSheet($i)->getSheetState()
  194. );
  195. }
  196. $objWriter->endElement();
  197. }
  198. /**
  199. * Write sheet
  200. *
  201. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  202. * @param string $pSheetname Sheet name
  203. * @param int $pSheetId Sheet id
  204. * @param int $pRelId Relationship ID
  205. * @param string $sheetState Sheet state (visible, hidden, veryHidden)
  206. * @throws PHPExcel_Writer_Exception
  207. */
  208. private function _writeSheet(PHPExcel_Shared_XMLWriter $objWriter = null, $pSheetname = '', $pSheetId = 1, $pRelId = 1, $sheetState = 'visible')
  209. {
  210. if ($pSheetname != '') {
  211. // Write sheet
  212. $objWriter->startElement('sheet');
  213. $objWriter->writeAttribute('name', $pSheetname);
  214. $objWriter->writeAttribute('sheetId', $pSheetId);
  215. if ($sheetState != 'visible' && $sheetState != '') {
  216. $objWriter->writeAttribute('state', $sheetState);
  217. }
  218. $objWriter->writeAttribute('r:id', 'rId' . $pRelId);
  219. $objWriter->endElement();
  220. } else {
  221. throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
  222. }
  223. }
  224. /**
  225. * Write Defined Names
  226. *
  227. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  228. * @param PHPExcel $pPHPExcel
  229. * @throws PHPExcel_Writer_Exception
  230. */
  231. private function _writeDefinedNames(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  232. {
  233. // Write defined names
  234. $objWriter->startElement('definedNames');
  235. // Named ranges
  236. if (count($pPHPExcel->getNamedRanges()) > 0) {
  237. // Named ranges
  238. $this->_writeNamedRanges($objWriter, $pPHPExcel);
  239. }
  240. // Other defined names
  241. $sheetCount = $pPHPExcel->getSheetCount();
  242. for ($i = 0; $i < $sheetCount; ++$i) {
  243. // definedName for autoFilter
  244. $this->_writeDefinedNameForAutofilter($objWriter, $pPHPExcel->getSheet($i), $i);
  245. // definedName for Print_Titles
  246. $this->_writeDefinedNameForPrintTitles($objWriter, $pPHPExcel->getSheet($i), $i);
  247. // definedName for Print_Area
  248. $this->_writeDefinedNameForPrintArea($objWriter, $pPHPExcel->getSheet($i), $i);
  249. }
  250. $objWriter->endElement();
  251. }
  252. /**
  253. * Write named ranges
  254. *
  255. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  256. * @param PHPExcel $pPHPExcel
  257. * @throws PHPExcel_Writer_Exception
  258. */
  259. private function _writeNamedRanges(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel)
  260. {
  261. // Loop named ranges
  262. $namedRanges = $pPHPExcel->getNamedRanges();
  263. foreach ($namedRanges as $namedRange) {
  264. $this->_writeDefinedNameForNamedRange($objWriter, $namedRange);
  265. }
  266. }
  267. /**
  268. * Write Defined Name for named range
  269. *
  270. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  271. * @param PHPExcel_NamedRange $pNamedRange
  272. * @throws PHPExcel_Writer_Exception
  273. */
  274. private function _writeDefinedNameForNamedRange(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_NamedRange $pNamedRange)
  275. {
  276. // definedName for named range
  277. $objWriter->startElement('definedName');
  278. $objWriter->writeAttribute('name', $pNamedRange->getName());
  279. if ($pNamedRange->getLocalOnly()) {
  280. $objWriter->writeAttribute('localSheetId', $pNamedRange->getScope()->getParent()->getIndex($pNamedRange->getScope()));
  281. }
  282. // Create absolute coordinate and write as raw text
  283. $range = PHPExcel_Cell::splitRange($pNamedRange->getRange());
  284. for ($i = 0; $i < count($range); $i++) {
  285. $range[$i][0] = '\'' . str_replace("'", "''", $pNamedRange->getWorksheet()->getTitle()) . '\'!' . PHPExcel_Cell::absoluteReference($range[$i][0]);
  286. if (isset($range[$i][1])) {
  287. $range[$i][1] = PHPExcel_Cell::absoluteReference($range[$i][1]);
  288. }
  289. }
  290. $range = PHPExcel_Cell::buildRange($range);
  291. $objWriter->writeRawData($range);
  292. $objWriter->endElement();
  293. }
  294. /**
  295. * Write Defined Name for autoFilter
  296. *
  297. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  298. * @param PHPExcel_Worksheet $pSheet
  299. * @param int $pSheetId
  300. * @throws PHPExcel_Writer_Exception
  301. */
  302. private function _writeDefinedNameForAutofilter(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  303. {
  304. // definedName for autoFilter
  305. $autoFilterRange = $pSheet->getAutoFilter()->getRange();
  306. if (!empty($autoFilterRange)) {
  307. $objWriter->startElement('definedName');
  308. $objWriter->writeAttribute('name', '_xlnm._FilterDatabase');
  309. $objWriter->writeAttribute('localSheetId', $pSheetId);
  310. $objWriter->writeAttribute('hidden', '1');
  311. // Create absolute coordinate and write as raw text
  312. $range = PHPExcel_Cell::splitRange($autoFilterRange);
  313. $range = $range[0];
  314. // Strip any worksheet ref so we can make the cell ref absolute
  315. if (strpos($range[0],'!') !== false) {
  316. list($ws,$range[0]) = explode('!',$range[0]);
  317. }
  318. $range[0] = PHPExcel_Cell::absoluteCoordinate($range[0]);
  319. $range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]);
  320. $range = implode(':', $range);
  321. $objWriter->writeRawData('\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . $range);
  322. $objWriter->endElement();
  323. }
  324. }
  325. /**
  326. * Write Defined Name for PrintTitles
  327. *
  328. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  329. * @param PHPExcel_Worksheet $pSheet
  330. * @param int $pSheetId
  331. * @throws PHPExcel_Writer_Exception
  332. */
  333. private function _writeDefinedNameForPrintTitles(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  334. {
  335. // definedName for PrintTitles
  336. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet() || $pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
  337. $objWriter->startElement('definedName');
  338. $objWriter->writeAttribute('name', '_xlnm.Print_Titles');
  339. $objWriter->writeAttribute('localSheetId', $pSheetId);
  340. // Setting string
  341. $settingString = '';
  342. // Columns to repeat
  343. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
  344. $repeat = $pSheet->getPageSetup()->getColumnsToRepeatAtLeft();
  345. $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1];
  346. }
  347. // Rows to repeat
  348. if ($pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
  349. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
  350. $settingString .= ',';
  351. }
  352. $repeat = $pSheet->getPageSetup()->getRowsToRepeatAtTop();
  353. $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1];
  354. }
  355. $objWriter->writeRawData($settingString);
  356. $objWriter->endElement();
  357. }
  358. }
  359. /**
  360. * Write Defined Name for PrintTitles
  361. *
  362. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  363. * @param PHPExcel_Worksheet $pSheet
  364. * @param int $pSheetId
  365. * @throws PHPExcel_Writer_Exception
  366. */
  367. private function _writeDefinedNameForPrintArea(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  368. {
  369. // definedName for PrintArea
  370. if ($pSheet->getPageSetup()->isPrintAreaSet()) {
  371. $objWriter->startElement('definedName');
  372. $objWriter->writeAttribute('name', '_xlnm.Print_Area');
  373. $objWriter->writeAttribute('localSheetId', $pSheetId);
  374. // Setting string
  375. $settingString = '';
  376. // Print area
  377. $printArea = PHPExcel_Cell::splitRange($pSheet->getPageSetup()->getPrintArea());
  378. $chunks = array();
  379. foreach ($printArea as $printAreaRect) {
  380. $printAreaRect[0] = PHPExcel_Cell::absoluteReference($printAreaRect[0]);
  381. $printAreaRect[1] = PHPExcel_Cell::absoluteReference($printAreaRect[1]);
  382. $chunks[] = '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . implode(':', $printAreaRect);
  383. }
  384. $objWriter->writeRawData(implode(',', $chunks));
  385. $objWriter->endElement();
  386. }
  387. }
  388. }