5 * Copyright (c) 2006 - 2014 PHPExcel
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.
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.
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
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##
30 * PHPExcel_Writer_Excel2007_Drawing
33 * @package PHPExcel_Writer_Excel2007
34 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
36 class PHPExcel_Writer_Excel2007_Drawing
extends PHPExcel_Writer_Excel2007_WriterPart
39 * Write drawings to XML format
41 * @param PHPExcel_Worksheet $pWorksheet
42 * @param int &$chartRef Chart ID
43 * @param boolean $includeCharts Flag indicating if we should include drawing details for charts
44 * @return string XML Output
45 * @throws PHPExcel_Writer_Exception
47 public function writeDrawings(PHPExcel_Worksheet
$pWorksheet = null, &$chartRef, $includeCharts = FALSE)
51 if ($this->getParentWriter()->getUseDiskCaching()) {
52 $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter
::STORAGE_DISK
, $this->getParentWriter()->getDiskCachingDirectory());
54 $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter
::STORAGE_MEMORY
);
58 $objWriter->startDocument('1.0','UTF-8','yes');
61 $objWriter->startElement('xdr:wsDr');
62 $objWriter->writeAttribute('xmlns:xdr', 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing');
63 $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
65 // Loop through images and write drawings
67 $iterator = $pWorksheet->getDrawingCollection()->getIterator();
68 while ($iterator->valid()) {
69 $this->_writeDrawing($objWriter, $iterator->current(), $i);
76 $chartCount = $pWorksheet->getChartCount();
77 // Loop through charts and write the chart position
78 if ($chartCount > 0) {
79 for ($c = 0; $c < $chartCount; ++
$c) {
80 $this->_writeChart($objWriter, $pWorksheet->getChartByIndex($c), $c+
$i);
86 $objWriter->endElement();
89 return $objWriter->getData();
93 * Write drawings to XML format
95 * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
96 * @param PHPExcel_Chart $pChart
97 * @param int $pRelationId
98 * @throws PHPExcel_Writer_Exception
100 public function _writeChart(PHPExcel_Shared_XMLWriter
$objWriter = null, PHPExcel_Chart
$pChart = null, $pRelationId = -1)
102 $tl = $pChart->getTopLeftPosition();
103 $tl['colRow'] = PHPExcel_Cell
::coordinateFromString($tl['cell']);
104 $br = $pChart->getBottomRightPosition();
105 $br['colRow'] = PHPExcel_Cell
::coordinateFromString($br['cell']);
107 $objWriter->startElement('xdr:twoCellAnchor');
109 $objWriter->startElement('xdr:from');
110 $objWriter->writeElement('xdr:col', PHPExcel_Cell
::columnIndexFromString($tl['colRow'][0]) - 1);
111 $objWriter->writeElement('xdr:colOff', PHPExcel_Shared_Drawing
::pixelsToEMU($tl['xOffset']));
112 $objWriter->writeElement('xdr:row', $tl['colRow'][1] - 1);
113 $objWriter->writeElement('xdr:rowOff', PHPExcel_Shared_Drawing
::pixelsToEMU($tl['yOffset']));
114 $objWriter->endElement();
115 $objWriter->startElement('xdr:to');
116 $objWriter->writeElement('xdr:col', PHPExcel_Cell
::columnIndexFromString($br['colRow'][0]) - 1);
117 $objWriter->writeElement('xdr:colOff', PHPExcel_Shared_Drawing
::pixelsToEMU($br['xOffset']));
118 $objWriter->writeElement('xdr:row', $br['colRow'][1] - 1);
119 $objWriter->writeElement('xdr:rowOff', PHPExcel_Shared_Drawing
::pixelsToEMU($br['yOffset']));
120 $objWriter->endElement();
122 $objWriter->startElement('xdr:graphicFrame');
123 $objWriter->writeAttribute('macro', '');
124 $objWriter->startElement('xdr:nvGraphicFramePr');
125 $objWriter->startElement('xdr:cNvPr');
126 $objWriter->writeAttribute('name', 'Chart '.$pRelationId);
127 $objWriter->writeAttribute('id', 1025 * $pRelationId);
128 $objWriter->endElement();
129 $objWriter->startElement('xdr:cNvGraphicFramePr');
130 $objWriter->startElement('a:graphicFrameLocks');
131 $objWriter->endElement();
132 $objWriter->endElement();
133 $objWriter->endElement();
135 $objWriter->startElement('xdr:xfrm');
136 $objWriter->startElement('a:off');
137 $objWriter->writeAttribute('x', '0');
138 $objWriter->writeAttribute('y', '0');
139 $objWriter->endElement();
140 $objWriter->startElement('a:ext');
141 $objWriter->writeAttribute('cx', '0');
142 $objWriter->writeAttribute('cy', '0');
143 $objWriter->endElement();
144 $objWriter->endElement();
146 $objWriter->startElement('a:graphic');
147 $objWriter->startElement('a:graphicData');
148 $objWriter->writeAttribute('uri', 'http://schemas.openxmlformats.org/drawingml/2006/chart');
149 $objWriter->startElement('c:chart');
150 $objWriter->writeAttribute('xmlns:c', 'http://schemas.openxmlformats.org/drawingml/2006/chart');
151 $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
152 $objWriter->writeAttribute('r:id', 'rId'.$pRelationId);
153 $objWriter->endElement();
154 $objWriter->endElement();
155 $objWriter->endElement();
156 $objWriter->endElement();
158 $objWriter->startElement('xdr:clientData');
159 $objWriter->endElement();
161 $objWriter->endElement();
165 * Write drawings to XML format
167 * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
168 * @param PHPExcel_Worksheet_BaseDrawing $pDrawing
169 * @param int $pRelationId
170 * @throws PHPExcel_Writer_Exception
172 public function _writeDrawing(PHPExcel_Shared_XMLWriter
$objWriter = null, PHPExcel_Worksheet_BaseDrawing
$pDrawing = null, $pRelationId = -1)
174 if ($pRelationId >= 0) {
176 $objWriter->startElement('xdr:oneCellAnchor');
178 $aCoordinates = PHPExcel_Cell
::coordinateFromString($pDrawing->getCoordinates());
179 $aCoordinates[0] = PHPExcel_Cell
::columnIndexFromString($aCoordinates[0]);
182 $objWriter->startElement('xdr:from');
183 $objWriter->writeElement('xdr:col', $aCoordinates[0] - 1);
184 $objWriter->writeElement('xdr:colOff', PHPExcel_Shared_Drawing
::pixelsToEMU($pDrawing->getOffsetX()));
185 $objWriter->writeElement('xdr:row', $aCoordinates[1] - 1);
186 $objWriter->writeElement('xdr:rowOff', PHPExcel_Shared_Drawing
::pixelsToEMU($pDrawing->getOffsetY()));
187 $objWriter->endElement();
190 $objWriter->startElement('xdr:ext');
191 $objWriter->writeAttribute('cx', PHPExcel_Shared_Drawing
::pixelsToEMU($pDrawing->getWidth()));
192 $objWriter->writeAttribute('cy', PHPExcel_Shared_Drawing
::pixelsToEMU($pDrawing->getHeight()));
193 $objWriter->endElement();
196 $objWriter->startElement('xdr:pic');
199 $objWriter->startElement('xdr:nvPicPr');
202 $objWriter->startElement('xdr:cNvPr');
203 $objWriter->writeAttribute('id', $pRelationId);
204 $objWriter->writeAttribute('name', $pDrawing->getName());
205 $objWriter->writeAttribute('descr', $pDrawing->getDescription());
206 $objWriter->endElement();
209 $objWriter->startElement('xdr:cNvPicPr');
212 $objWriter->startElement('a:picLocks');
213 $objWriter->writeAttribute('noChangeAspect', '1');
214 $objWriter->endElement();
216 $objWriter->endElement();
218 $objWriter->endElement();
221 $objWriter->startElement('xdr:blipFill');
224 $objWriter->startElement('a:blip');
225 $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
226 $objWriter->writeAttribute('r:embed', 'rId' . $pRelationId);
227 $objWriter->endElement();
230 $objWriter->startElement('a:stretch');
231 $objWriter->writeElement('a:fillRect', null);
232 $objWriter->endElement();
234 $objWriter->endElement();
237 $objWriter->startElement('xdr:spPr');
240 $objWriter->startElement('a:xfrm');
241 $objWriter->writeAttribute('rot', PHPExcel_Shared_Drawing
::degreesToAngle($pDrawing->getRotation()));
242 $objWriter->endElement();
245 $objWriter->startElement('a:prstGeom');
246 $objWriter->writeAttribute('prst', 'rect');
249 $objWriter->writeElement('a:avLst', null);
251 $objWriter->endElement();
254 // $objWriter->startElement('a:solidFill');
257 // $objWriter->startElement('a:srgbClr');
258 // $objWriter->writeAttribute('val', 'FFFFFF');
262 // $objWriter->startElement('a:shade');
263 // $objWriter->writeAttribute('val', '85000');
264 // $objWriter->endElement();
267 // $objWriter->endElement();
269 // $objWriter->endElement();
272 $objWriter->startElement('a:ln');
273 $objWriter->writeAttribute('w', '88900');
274 $objWriter->writeAttribute('cap', 'sq');
277 $objWriter->startElement('a:solidFill');
280 $objWriter->startElement('a:srgbClr');
281 $objWriter->writeAttribute('val', 'FFFFFF');
282 $objWriter->endElement();
284 $objWriter->endElement();
287 $objWriter->startElement('a:miter');
288 $objWriter->writeAttribute('lim', '800000');
289 $objWriter->endElement();
291 $objWriter->endElement();
294 if ($pDrawing->getShadow()->getVisible()) {
296 $objWriter->startElement('a:effectLst');
299 $objWriter->startElement('a:outerShdw');
300 $objWriter->writeAttribute('blurRad', PHPExcel_Shared_Drawing
::pixelsToEMU($pDrawing->getShadow()->getBlurRadius()));
301 $objWriter->writeAttribute('dist', PHPExcel_Shared_Drawing
::pixelsToEMU($pDrawing->getShadow()->getDistance()));
302 $objWriter->writeAttribute('dir', PHPExcel_Shared_Drawing
::degreesToAngle($pDrawing->getShadow()->getDirection()));
303 $objWriter->writeAttribute('algn', $pDrawing->getShadow()->getAlignment());
304 $objWriter->writeAttribute('rotWithShape', '0');
307 $objWriter->startElement('a:srgbClr');
308 $objWriter->writeAttribute('val', $pDrawing->getShadow()->getColor()->getRGB());
311 $objWriter->startElement('a:alpha');
312 $objWriter->writeAttribute('val', $pDrawing->getShadow()->getAlpha() * 1000);
313 $objWriter->endElement();
315 $objWriter->endElement();
317 $objWriter->endElement();
319 $objWriter->endElement();
324 $objWriter->startElement('a:scene3d');
327 $objWriter->startElement('a:camera');
328 $objWriter->writeAttribute('prst', 'orthographicFront');
329 $objWriter->endElement();
332 $objWriter->startElement('a:lightRig');
333 $objWriter->writeAttribute('rig', 'twoPt');
334 $objWriter->writeAttribute('dir', 't');
337 $objWriter->startElement('a:rot');
338 $objWriter->writeAttribute('lat', '0');
339 $objWriter->writeAttribute('lon', '0');
340 $objWriter->writeAttribute('rev', '0');
341 $objWriter->endElement();
343 $objWriter->endElement();
345 $objWriter->endElement();
349 $objWriter->startElement('a:sp3d');
352 $objWriter->startElement('a:bevelT');
353 $objWriter->writeAttribute('w', '25400');
354 $objWriter->writeAttribute('h', '19050');
355 $objWriter->endElement();
358 $objWriter->startElement('a:contourClr');
361 $objWriter->startElement('a:srgbClr');
362 $objWriter->writeAttribute('val', 'FFFFFF');
363 $objWriter->endElement();
365 $objWriter->endElement();
367 $objWriter->endElement();
369 $objWriter->endElement();
371 $objWriter->endElement();
374 $objWriter->writeElement('xdr:clientData', null);
376 $objWriter->endElement();
378 throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
383 * Write VML header/footer images to XML format
385 * @param PHPExcel_Worksheet $pWorksheet
386 * @return string XML Output
387 * @throws PHPExcel_Writer_Exception
389 public function writeVMLHeaderFooterImages(PHPExcel_Worksheet
$pWorksheet = null)
393 if ($this->getParentWriter()->getUseDiskCaching()) {
394 $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter
::STORAGE_DISK
, $this->getParentWriter()->getDiskCachingDirectory());
396 $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter
::STORAGE_MEMORY
);
400 $objWriter->startDocument('1.0','UTF-8','yes');
402 // Header/footer images
403 $images = $pWorksheet->getHeaderFooter()->getImages();
406 $objWriter->startElement('xml');
407 $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
408 $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
409 $objWriter->writeAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
412 $objWriter->startElement('o:shapelayout');
413 $objWriter->writeAttribute('v:ext', 'edit');
416 $objWriter->startElement('o:idmap');
417 $objWriter->writeAttribute('v:ext', 'edit');
418 $objWriter->writeAttribute('data', '1');
419 $objWriter->endElement();
421 $objWriter->endElement();
424 $objWriter->startElement('v:shapetype');
425 $objWriter->writeAttribute('id', '_x0000_t75');
426 $objWriter->writeAttribute('coordsize', '21600,21600');
427 $objWriter->writeAttribute('o:spt', '75');
428 $objWriter->writeAttribute('o:preferrelative', 't');
429 $objWriter->writeAttribute('path', 'm@4@5l@4@11@9@11@9@5xe');
430 $objWriter->writeAttribute('filled', 'f');
431 $objWriter->writeAttribute('stroked', 'f');
434 $objWriter->startElement('v:stroke');
435 $objWriter->writeAttribute('joinstyle', 'miter');
436 $objWriter->endElement();
439 $objWriter->startElement('v:formulas');
442 $objWriter->startElement('v:f');
443 $objWriter->writeAttribute('eqn', 'if lineDrawn pixelLineWidth 0');
444 $objWriter->endElement();
447 $objWriter->startElement('v:f');
448 $objWriter->writeAttribute('eqn', 'sum @0 1 0');
449 $objWriter->endElement();
452 $objWriter->startElement('v:f');
453 $objWriter->writeAttribute('eqn', 'sum 0 0 @1');
454 $objWriter->endElement();
457 $objWriter->startElement('v:f');
458 $objWriter->writeAttribute('eqn', 'prod @2 1 2');
459 $objWriter->endElement();
462 $objWriter->startElement('v:f');
463 $objWriter->writeAttribute('eqn', 'prod @3 21600 pixelWidth');
464 $objWriter->endElement();
467 $objWriter->startElement('v:f');
468 $objWriter->writeAttribute('eqn', 'prod @3 21600 pixelHeight');
469 $objWriter->endElement();
472 $objWriter->startElement('v:f');
473 $objWriter->writeAttribute('eqn', 'sum @0 0 1');
474 $objWriter->endElement();
477 $objWriter->startElement('v:f');
478 $objWriter->writeAttribute('eqn', 'prod @6 1 2');
479 $objWriter->endElement();
482 $objWriter->startElement('v:f');
483 $objWriter->writeAttribute('eqn', 'prod @7 21600 pixelWidth');
484 $objWriter->endElement();
487 $objWriter->startElement('v:f');
488 $objWriter->writeAttribute('eqn', 'sum @8 21600 0');
489 $objWriter->endElement();
492 $objWriter->startElement('v:f');
493 $objWriter->writeAttribute('eqn', 'prod @7 21600 pixelHeight');
494 $objWriter->endElement();
497 $objWriter->startElement('v:f');
498 $objWriter->writeAttribute('eqn', 'sum @10 21600 0');
499 $objWriter->endElement();
501 $objWriter->endElement();
504 $objWriter->startElement('v:path');
505 $objWriter->writeAttribute('o:extrusionok', 'f');
506 $objWriter->writeAttribute('gradientshapeok', 't');
507 $objWriter->writeAttribute('o:connecttype', 'rect');
508 $objWriter->endElement();
511 $objWriter->startElement('o:lock');
512 $objWriter->writeAttribute('v:ext', 'edit');
513 $objWriter->writeAttribute('aspectratio', 't');
514 $objWriter->endElement();
516 $objWriter->endElement();
518 // Loop through images
519 foreach ($images as $key => $value) {
520 $this->_writeVMLHeaderFooterImage($objWriter, $key, $value);
523 $objWriter->endElement();
526 return $objWriter->getData();
530 * Write VML comment to XML format
532 * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
533 * @param string $pReference Reference
534 * @param PHPExcel_Worksheet_HeaderFooterDrawing $pImage Image
535 * @throws PHPExcel_Writer_Exception
537 public function _writeVMLHeaderFooterImage(PHPExcel_Shared_XMLWriter
$objWriter = null, $pReference = '', PHPExcel_Worksheet_HeaderFooterDrawing
$pImage = null)
539 // Calculate object id
540 preg_match('{(\d+)}', md5($pReference), $m);
541 $id = 1500 +
(substr($m[1], 0, 2) * 1);
544 $width = $pImage->getWidth();
545 $height = $pImage->getHeight();
546 $marginLeft = $pImage->getOffsetX();
547 $marginTop = $pImage->getOffsetY();
550 $objWriter->startElement('v:shape');
551 $objWriter->writeAttribute('id', $pReference);
552 $objWriter->writeAttribute('o:spid', '_x0000_s' . $id);
553 $objWriter->writeAttribute('type', '#_x0000_t75');
554 $objWriter->writeAttribute('style', "position:absolute;margin-left:{$marginLeft}px;margin-top:{$marginTop}px;width:{$width}px;height:{$height}px;z-index:1");
557 $objWriter->startElement('v:imagedata');
558 $objWriter->writeAttribute('o:relid', 'rId' . $pReference);
559 $objWriter->writeAttribute('o:title', $pImage->getName());
560 $objWriter->endElement();
563 $objWriter->startElement('o:lock');
564 $objWriter->writeAttribute('v:ext', 'edit');
565 $objWriter->writeAttribute('rotation', 't');
566 $objWriter->endElement();
568 $objWriter->endElement();
573 * Get an array of all drawings
575 * @param PHPExcel $pPHPExcel
576 * @return PHPExcel_Worksheet_Drawing[] All drawings in PHPExcel
577 * @throws PHPExcel_Writer_Exception
579 public function allDrawings(PHPExcel
$pPHPExcel = null)
581 // Get an array of all drawings
582 $aDrawings = array();
584 // Loop through PHPExcel
585 $sheetCount = $pPHPExcel->getSheetCount();
586 for ($i = 0; $i < $sheetCount; ++
$i) {
587 // Loop through images and add to array
588 $iterator = $pPHPExcel->getSheet($i)->getDrawingCollection()->getIterator();
589 while ($iterator->valid()) {
590 $aDrawings[] = $iterator->current();