File Source for Workbook.php

PHPExcel_Writer_Excel2007

# Source for file Workbook.php

Documentation is available at Workbook.php

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

Documentation generated on Sun, 27 Feb 2011 16:37:02 -0800 by phpDocumentor 1.4.3