File Source for Excel5.php

PHPExcel_Writer_Excel5

# Source for file Excel5.php

Documentation is available at Excel5.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_Excel5
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_Excel5
31. *
32. * @category PHPExcel
33. * @package PHPExcel_Writer_Excel5
34. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
35. */
36. class PHPExcel_Writer_Excel5 implements PHPExcel_Writer_IWriter
37. {
38. /**
39. * Pre-calculate formulas
40. *
41. * @var boolean
42. */
43. private $_preCalculateFormulas = true;
45. /**
46. * PHPExcel object
47. *
48. * @var PHPExcel
49. */
50. private $_phpExcel;
52. /**
53. * The BIFF version of the written Excel file, BIFF5 = 0x0500, BIFF8 = 0x0600
54. *
55. * @var integer
56. */
57. private $_BIFF_version = 0x0600;
59. /**
60. * Total number of shared strings in workbook
61. *
62. * @var int
63. */
64. private $_str_total = 0;
66. /**
67. * Number of unique shared strings in workbook
68. *
69. * @var int
70. */
71. private $_str_unique = 0;
73. /**
74. * Array of unique shared strings in workbook
75. *
76. * @var array
77. */
78. private $_str_table = array();
80. /**
81. * Color cache. Mapping between RGB value and color index.
82. *
83. * @var array
84. */
85. private $_colors;
87. /**
88. * Formula parser
89. *
90. * @var PHPExcel_Writer_Excel5_Parser
91. */
92. private $_parser;
94. /**
95. * Identifier clusters for drawings. Used in MSODRAWINGGROUP record.
96. *
97. * @var array
98. */
99. private $_IDCLs;
102. /**
103. * Create a new PHPExcel_Writer_Excel5
104. *
105. * @param PHPExcel $phpExcel PHPExcel object
106. */
107. public function __construct(PHPExcel $phpExcel) {
108. $this->_phpExcel = $phpExcel;
110. $this->_parser = new PHPExcel_Writer_Excel5_Parser($this->_BIFF_version);
111. }
113. /**
114. * Save PHPExcel to file
115. *
116. * @param string $pFileName
117. * @throws Exception
118. */
119. public function save($pFilename = null) {
121. // garbage collect
122. $this->_phpExcel->garbageCollect();
124. $saveDebugLog = PHPExcel_Calculation:: getInstance()->writeDebugLog;
125. PHPExcel_Calculation:: getInstance()->writeDebugLog = false;
126. $saveDateReturnType = PHPExcel_Calculation_Functions:: getReturnDateType();
127. PHPExcel_Calculation_Functions:: setReturnDateType( PHPExcel_Calculation_Functions:: RETURNDATE_EXCEL);
129. // initialize colors array
130. $this->_colors = array();
132. // Initialise workbook writer
133. $this->_writerWorkbook = new PHPExcel_Writer_Excel5_Workbook($this->_phpExcel, $this->_BIFF_version,
134. $this->_str_total, $this->_str_unique, $this->_str_table, $this->_colors, $this->_parser);
136. // Initialise worksheet writers
137. $countSheets = $this->_phpExcel->getSheetCount();
138. for ($i = 0; $i < $countSheets; ++$i) {
139. $this->_writerWorksheets[$i] = new PHPExcel_Writer_Excel5_Worksheet($this->_BIFF_version,
140. $this->_str_total, $this->_str_unique,
141. $this->_str_table, $this->_colors,
142. $this->_parser,
143. $this->_preCalculateFormulas,
144. $this->_phpExcel->getSheet($i));
145. }
147. // build Escher objects. Escher objects for workbooks needs to be build before Escher object for workbook.
148. $this->_buildWorksheetEschers();
149. $this->_buildWorkbookEscher();
151. // add 15 identical cell style Xfs
152. // for now, we use the first cellXf instead of cellStyleXf
153. $cellXfCollection = $this->_phpExcel->getCellXfCollection();
154. for ($i = 0; $i < 15; ++$i) {
155. $this->_writerWorkbook->addXfWriter($cellXfCollection[0], true);
156. }
158. // add all the cell Xfs
159. foreach ($this->_phpExcel->getCellXfCollection() as $style) {
160. $this->_writerWorkbook->addXfWriter($style, false);
161. }
163. // initialize OLE file
164. $workbookStreamName = ($this->_BIFF_version == 0x0600) ? 'Workbook' : 'Book';
165. $OLE = new PHPExcel_Shared_OLE_PPS_File( PHPExcel_Shared_OLE:: Asc2Ucs($workbookStreamName));
167. // Write the worksheet streams before the global workbook stream,
168. // because the byte sizes of these are needed in the global workbook stream
169. $worksheetSizes = array();
170. for ($i = 0; $i < $countSheets; ++$i) {
171. $this->_writerWorksheets[$i]->close();
172. $worksheetSizes[] = $this->_writerWorksheets[$i]->_datasize;
173. }
175. // add binary data for global workbook stream
176. $OLE->append( $this->_writerWorkbook->writeWorkbook($worksheetSizes) );
178. // add binary data for sheet streams
179. for ($i = 0; $i < $countSheets; ++$i) {
180. $OLE->append($this->_writerWorksheets[$i]->getData());
181. }
183. $root = new PHPExcel_Shared_OLE_PPS_Root( time(), time(), array($OLE));
184. // save the OLE file
185. $res = $root->save($pFilename);
187. PHPExcel_Calculation_Functions:: setReturnDateType($saveDateReturnType);
188. PHPExcel_Calculation:: getInstance()->writeDebugLog = $saveDebugLog;
189. }
191. /**
192. * Set temporary storage directory
193. *
194. * @deprecated
195. * @param string $pValue Temporary storage directory
196. * @throws Exception Exception when directory does not exist
197. * @return PHPExcel_Writer_Excel5
198. */
199. public function setTempDir($pValue = '') {
200. return $this;
201. }
203. /**
204. * Get Pre-Calculate Formulas
205. *
206. * @return boolean
207. */
208. public function getPreCalculateFormulas() {
209. return $this->_preCalculateFormulas;
210. }
212. /**
213. * Set Pre-Calculate Formulas
214. *
215. * @param boolean $pValue Pre-Calculate Formulas?
216. */
217. public function setPreCalculateFormulas($pValue = true) {
218. $this->_preCalculateFormulas = $pValue;
219. }
221. private function _buildWorksheetEschers()
222. {
223. // 1-based index to BstoreContainer
224. $blipIndex = 0;
226. foreach ($this->_phpExcel->getAllsheets() as $sheet) {
227. // sheet index
228. $sheetIndex = $sheet->getParent()->getIndex($sheet);
230. $escher = null;
232. // check if there are any shapes for this sheet
233. if ( count($sheet->getDrawingCollection()) == 0) {
234. continue;
235. }
237. // create intermediate Escher object
238. $escher = new PHPExcel_Shared_Escher();
240. // dgContainer
241. $dgContainer = new PHPExcel_Shared_Escher_DgContainer();
243. // set the drawing index (we use sheet index + 1)
244. $dgId = $sheet->getParent()->getIndex($sheet) + 1;
245. $dgContainer->setDgId($dgId);
246. $escher->setDgContainer($dgContainer);
248. // spgrContainer
249. $spgrContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer();
250. $dgContainer->setSpgrContainer($spgrContainer);
252. // add one shape which is the group shape
253. $spContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
254. $spContainer->setSpgr(true);
255. $spContainer->setSpType(0);
256. $spContainer->setSpId(($sheet->getParent()->getIndex($sheet) + 1) << 10);
257. $spgrContainer->addChild($spContainer);
259. // add the shapes
261. $countShapes[$sheetIndex] = 0; // count number of shapes (minus group shape), in sheet
263. foreach ($sheet->getDrawingCollection() as $drawing) {
264. ++$blipIndex;
266. ++$countShapes[$sheetIndex];
268. // add the shape
269. $spContainer = new PHPExcel_Shared_Escher_DgContainer_SpgrContainer_SpContainer();
271. // set the shape type
272. $spContainer->setSpType(0x004B);
274. // set the shape index (we combine 1-based sheet index and $countShapes to create unique shape index)
275. $reducedSpId = $countShapes[$sheetIndex];
276. $spId = $reducedSpId
277. | ($sheet->getParent()->getIndex($sheet) + 1) << 10;
278. $spContainer->setSpId($spId);
280. // keep track of last reducedSpId
281. $lastReducedSpId = $reducedSpId;
283. // keep track of last spId
284. $lastSpId = $spId;
286. // set the BLIP index
287. $spContainer->setOPT(0x4104, $blipIndex);
289. // set coordinates and offsets, client anchor
290. $coordinates = $drawing->getCoordinates();
291. $offsetX = $drawing->getOffsetX();
292. $offsetY = $drawing->getOffsetY();
293. $width = $drawing->getWidth();
294. $height = $drawing->getHeight();
296. $twoAnchor = PHPExcel_Shared_Excel5:: oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height);
298. $spContainer->setStartCoordinates($twoAnchor['startCoordinates']);
299. $spContainer->setStartOffsetX($twoAnchor['startOffsetX']);
300. $spContainer->setStartOffsetY($twoAnchor['startOffsetY']);
301. $spContainer->setEndCoordinates($twoAnchor['endCoordinates']);
302. $spContainer->setEndOffsetX($twoAnchor['endOffsetX']);
303. $spContainer->setEndOffsetY($twoAnchor['endOffsetY']);
305. $spgrContainer->addChild($spContainer);
306. }
308. // identifier clusters, used for workbook Escher object
309. $this->_IDCLs[$dgId] = $lastReducedSpId;
311. // set last shape index
312. $dgContainer->setLastSpId($lastSpId);
314. // set the Escher object
315. $this->_writerWorksheets[$sheetIndex]->setEscher($escher);
316. }
317. }
319. /**
320. * Build the Escher object corresponding to the MSODRAWINGGROUP record
321. */
322. private function _buildWorkbookEscher()
323. {
324. $escher = null;
326. // any drawings in this workbook?
327. $found = false;
328. foreach ($this->_phpExcel->getAllSheets() as $sheet) {
329. if ( count($sheet->getDrawingCollection()) > 0) {
330. $found = true;
331. }
332. }
334. // nothing to do if there are no drawings
335. if (!$found) {
336. return;
337. }
339. // if we reach here, then there are drawings in the workbook
340. $escher = new PHPExcel_Shared_Escher();
342. // dggContainer
343. $dggContainer = new PHPExcel_Shared_Escher_DggContainer();
344. $escher->setDggContainer($dggContainer);
346. // set IDCLs (identifier clusters)
347. $dggContainer->setIDCLs($this->_IDCLs);
349. // this loop is for determining maximum shape identifier of all drawing
350. $spIdMax = 0;
351. $totalCountShapes = 0;
352. $countDrawings = 0;
354. foreach ($this->_phpExcel->getAllsheets() as $sheet) {
355. $sheetCountShapes = 0; // count number of shapes (minus group shape), in sheet
357. if ( count($sheet->getDrawingCollection()) > 0) {
358. ++$countDrawings;
360. foreach ($sheet->getDrawingCollection() as $drawing) {
361. ++$sheetCountShapes;
362. ++$totalCountShapes;
364. $spId = $sheetCountShapes
365. | ($this->_phpExcel->getIndex($sheet) + 1) << 10;
366. $spIdMax = max($spId, $spIdMax);
367. }
368. }
369. }
371. $dggContainer->setSpIdMax($spIdMax + 1);
372. $dggContainer->setCDgSaved($countDrawings);
373. $dggContainer->setCSpSaved($totalCountShapes + $countDrawings); // total number of shapes incl. one group shapes per drawing
375. // bstoreContainer
376. $bstoreContainer = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer();
377. $dggContainer->setBstoreContainer($bstoreContainer);
379. // the BSE's (all the images)
380. foreach ($this->_phpExcel->getAllsheets() as $sheet) {
381. foreach ($sheet->getDrawingCollection() as $drawing) {
382. if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
384. $filename = $drawing->getPath();
386. list($imagesx, $imagesy, $imageFormat) = getimagesize($filename);
388. switch ($imageFormat) {
390. case 1: // GIF, not supported by BIFF8, we convert to PNG
391. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE:: BLIPTYPE_PNG;
392. ob_start();
393. imagepng( imagecreatefromgif($filename));
394. $blipData = ob_get_contents();
395. ob_end_clean();
396. break;
398. case 2: // JPEG
399. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE:: BLIPTYPE_JPEG;
400. $blipData = file_get_contents($filename);
401. break;
403. case 3: // PNG
404. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE:: BLIPTYPE_PNG;
405. $blipData = file_get_contents($filename);
406. break;
408. case 6: // Windows DIB (BMP), we convert to PNG
409. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE:: BLIPTYPE_PNG;
410. ob_start();
411. imagepng( PHPExcel_Shared_Drawing:: imagecreatefrombmp($filename));
412. $blipData = ob_get_contents();
413. ob_end_clean();
414. break;
416. default: continue 2;
418. }
420. $blip = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
421. $blip->setData($blipData);
423. $BSE = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
424. $BSE->setBlipType($blipType);
425. $BSE->setBlip($blip);
427. $bstoreContainer->addBSE($BSE);
429. } else if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
431. switch ($drawing->getRenderingFunction()) {
433. case PHPExcel_Worksheet_MemoryDrawing:: RENDERING_JPEG:
434. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE:: BLIPTYPE_JPEG;
435. $renderingFunction = 'imagejpeg';
436. break;
438. case PHPExcel_Worksheet_MemoryDrawing:: RENDERING_GIF:
439. case PHPExcel_Worksheet_MemoryDrawing:: RENDERING_PNG:
440. case PHPExcel_Worksheet_MemoryDrawing:: RENDERING_DEFAULT:
441. $blipType = PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE:: BLIPTYPE_PNG;
442. $renderingFunction = 'imagepng';
443. break;
445. }
447. ob_start();
448. call_user_func($renderingFunction, $drawing->getImageResource());
449. $blipData = ob_get_contents();
450. ob_end_clean();
452. $blip = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE_Blip();
453. $blip->setData($blipData);
455. $BSE = new PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE();
456. $BSE->setBlipType($blipType);
457. $BSE->setBlip($blip);
459. $bstoreContainer->addBSE($BSE);
460. }
461. }
462. }
464. // Set the Escher object
465. $this->_writerWorkbook->setEscher($escher);
466. }
468. }

Documentation generated on Sun, 27 Feb 2011 16:31:13 -0800 by phpDocumentor 1.4.3