No Description

Workbook.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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_Excel2007
  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_Excel2007_Workbook
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_Excel2007
  32. * @copyright Copyright (c) 2006 - 2014 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. // Set the calcid to a higher value than Excel itself will use, otherwise Excel will always recalc
  169. // If MS Excel does do a recalc, then users opening a file in MS Excel will be prompted to save on exit
  170. // because the file has changed
  171. $objWriter->writeAttribute('calcId', '999999');
  172. $objWriter->writeAttribute('calcMode', 'auto');
  173. // fullCalcOnLoad isn't needed if we've recalculating for the save
  174. $objWriter->writeAttribute('calcCompleted', ($recalcRequired) ? 1 : 0);
  175. $objWriter->writeAttribute('fullCalcOnLoad', ($recalcRequired) ? 0 : 1);
  176. $objWriter->endElement();
  177. }
  178. /**
  179. * Write sheets
  180. *
  181. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  182. * @param PHPExcel $pPHPExcel
  183. * @throws PHPExcel_Writer_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. $pPHPExcel->getSheet($i)->getSheetState()
  198. );
  199. }
  200. $objWriter->endElement();
  201. }
  202. /**
  203. * Write sheet
  204. *
  205. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  206. * @param string $pSheetname Sheet name
  207. * @param int $pSheetId Sheet id
  208. * @param int $pRelId Relationship ID
  209. * @param string $sheetState Sheet state (visible, hidden, veryHidden)
  210. * @throws PHPExcel_Writer_Exception
  211. */
  212. private function _writeSheet(PHPExcel_Shared_XMLWriter $objWriter = null, $pSheetname = '', $pSheetId = 1, $pRelId = 1, $sheetState = 'visible')
  213. {
  214. if ($pSheetname != '') {
  215. // Write sheet
  216. $objWriter->startElement('sheet');
  217. $objWriter->writeAttribute('name', $pSheetname);
  218. $objWriter->writeAttribute('sheetId', $pSheetId);
  219. if ($sheetState != 'visible' && $sheetState != '') {
  220. $objWriter->writeAttribute('state', $sheetState);
  221. }
  222. $objWriter->writeAttribute('r:id', 'rId' . $pRelId);
  223. $objWriter->endElement();
  224. } else {
  225. throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
  226. }
  227. }
  228. /**
  229. * Write Defined Names
  230. *
  231. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  232. * @param PHPExcel $pPHPExcel
  233. * @throws PHPExcel_Writer_Exception
  234. */
  235. private function _writeDefinedNames(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel = null)
  236. {
  237. // Write defined names
  238. $objWriter->startElement('definedNames');
  239. // Named ranges
  240. if (count($pPHPExcel->getNamedRanges()) > 0) {
  241. // Named ranges
  242. $this->_writeNamedRanges($objWriter, $pPHPExcel);
  243. }
  244. // Other defined names
  245. $sheetCount = $pPHPExcel->getSheetCount();
  246. for ($i = 0; $i < $sheetCount; ++$i) {
  247. // definedName for autoFilter
  248. $this->_writeDefinedNameForAutofilter($objWriter, $pPHPExcel->getSheet($i), $i);
  249. // definedName for Print_Titles
  250. $this->_writeDefinedNameForPrintTitles($objWriter, $pPHPExcel->getSheet($i), $i);
  251. // definedName for Print_Area
  252. $this->_writeDefinedNameForPrintArea($objWriter, $pPHPExcel->getSheet($i), $i);
  253. }
  254. $objWriter->endElement();
  255. }
  256. /**
  257. * Write named ranges
  258. *
  259. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  260. * @param PHPExcel $pPHPExcel
  261. * @throws PHPExcel_Writer_Exception
  262. */
  263. private function _writeNamedRanges(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel $pPHPExcel)
  264. {
  265. // Loop named ranges
  266. $namedRanges = $pPHPExcel->getNamedRanges();
  267. foreach ($namedRanges as $namedRange) {
  268. $this->_writeDefinedNameForNamedRange($objWriter, $namedRange);
  269. }
  270. }
  271. /**
  272. * Write Defined Name for named range
  273. *
  274. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  275. * @param PHPExcel_NamedRange $pNamedRange
  276. * @throws PHPExcel_Writer_Exception
  277. */
  278. private function _writeDefinedNameForNamedRange(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_NamedRange $pNamedRange)
  279. {
  280. // definedName for named range
  281. $objWriter->startElement('definedName');
  282. $objWriter->writeAttribute('name', $pNamedRange->getName());
  283. if ($pNamedRange->getLocalOnly()) {
  284. $objWriter->writeAttribute('localSheetId', $pNamedRange->getScope()->getParent()->getIndex($pNamedRange->getScope()));
  285. }
  286. // Create absolute coordinate and write as raw text
  287. $range = PHPExcel_Cell::splitRange($pNamedRange->getRange());
  288. for ($i = 0; $i < count($range); $i++) {
  289. $range[$i][0] = '\'' . str_replace("'", "''", $pNamedRange->getWorksheet()->getTitle()) . '\'!' . PHPExcel_Cell::absoluteReference($range[$i][0]);
  290. if (isset($range[$i][1])) {
  291. $range[$i][1] = PHPExcel_Cell::absoluteReference($range[$i][1]);
  292. }
  293. }
  294. $range = PHPExcel_Cell::buildRange($range);
  295. $objWriter->writeRawData($range);
  296. $objWriter->endElement();
  297. }
  298. /**
  299. * Write Defined Name for autoFilter
  300. *
  301. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  302. * @param PHPExcel_Worksheet $pSheet
  303. * @param int $pSheetId
  304. * @throws PHPExcel_Writer_Exception
  305. */
  306. private function _writeDefinedNameForAutofilter(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  307. {
  308. // definedName for autoFilter
  309. $autoFilterRange = $pSheet->getAutoFilter()->getRange();
  310. if (!empty($autoFilterRange)) {
  311. $objWriter->startElement('definedName');
  312. $objWriter->writeAttribute('name', '_xlnm._FilterDatabase');
  313. $objWriter->writeAttribute('localSheetId', $pSheetId);
  314. $objWriter->writeAttribute('hidden', '1');
  315. // Create absolute coordinate and write as raw text
  316. $range = PHPExcel_Cell::splitRange($autoFilterRange);
  317. $range = $range[0];
  318. // Strip any worksheet ref so we can make the cell ref absolute
  319. if (strpos($range[0],'!') !== false) {
  320. list($ws,$range[0]) = explode('!',$range[0]);
  321. }
  322. $range[0] = PHPExcel_Cell::absoluteCoordinate($range[0]);
  323. $range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]);
  324. $range = implode(':', $range);
  325. $objWriter->writeRawData('\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . $range);
  326. $objWriter->endElement();
  327. }
  328. }
  329. /**
  330. * Write Defined Name for PrintTitles
  331. *
  332. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  333. * @param PHPExcel_Worksheet $pSheet
  334. * @param int $pSheetId
  335. * @throws PHPExcel_Writer_Exception
  336. */
  337. private function _writeDefinedNameForPrintTitles(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  338. {
  339. // definedName for PrintTitles
  340. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet() || $pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
  341. $objWriter->startElement('definedName');
  342. $objWriter->writeAttribute('name', '_xlnm.Print_Titles');
  343. $objWriter->writeAttribute('localSheetId', $pSheetId);
  344. // Setting string
  345. $settingString = '';
  346. // Columns to repeat
  347. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
  348. $repeat = $pSheet->getPageSetup()->getColumnsToRepeatAtLeft();
  349. $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1];
  350. }
  351. // Rows to repeat
  352. if ($pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) {
  353. if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) {
  354. $settingString .= ',';
  355. }
  356. $repeat = $pSheet->getPageSetup()->getRowsToRepeatAtTop();
  357. $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1];
  358. }
  359. $objWriter->writeRawData($settingString);
  360. $objWriter->endElement();
  361. }
  362. }
  363. /**
  364. * Write Defined Name for PrintTitles
  365. *
  366. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  367. * @param PHPExcel_Worksheet $pSheet
  368. * @param int $pSheetId
  369. * @throws PHPExcel_Writer_Exception
  370. */
  371. private function _writeDefinedNameForPrintArea(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, $pSheetId = 0)
  372. {
  373. // definedName for PrintArea
  374. if ($pSheet->getPageSetup()->isPrintAreaSet()) {
  375. $objWriter->startElement('definedName');
  376. $objWriter->writeAttribute('name', '_xlnm.Print_Area');
  377. $objWriter->writeAttribute('localSheetId', $pSheetId);
  378. // Setting string
  379. $settingString = '';
  380. // Print area
  381. $printArea = PHPExcel_Cell::splitRange($pSheet->getPageSetup()->getPrintArea());
  382. $chunks = array();
  383. foreach ($printArea as $printAreaRect) {
  384. $printAreaRect[0] = PHPExcel_Cell::absoluteReference($printAreaRect[0]);
  385. $printAreaRect[1] = PHPExcel_Cell::absoluteReference($printAreaRect[1]);
  386. $chunks[] = '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . implode(':', $printAreaRect);
  387. }
  388. $objWriter->writeRawData(implode(',', $chunks));
  389. $objWriter->endElement();
  390. }
  391. }
  392. }