1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27:
28:
29: 30: 31: 32: 33: 34: 35:
36: class PHPExcel_Writer_Excel2007 extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
37: {
38: 39: 40: 41: 42: 43: 44: 45: 46:
47: protected $_preCalculateFormulas = FALSE;
48:
49: 50: 51: 52: 53:
54: private $_office2003compatibility = false;
55:
56: 57: 58: 59: 60:
61: private $_writerParts = array();
62:
63: 64: 65: 66: 67:
68: private $_spreadSheet;
69:
70: 71: 72: 73: 74:
75: private $_stringTable = array();
76:
77: 78: 79: 80: 81:
82: private $_stylesConditionalHashTable;
83:
84: 85: 86: 87: 88:
89: private $_styleHashTable;
90:
91: 92: 93: 94: 95:
96: private $_fillHashTable;
97:
98: 99: 100: 101: 102:
103: private $_fontHashTable;
104:
105: 106: 107: 108: 109:
110: private $_bordersHashTable ;
111:
112: 113: 114: 115: 116:
117: private $_numFmtHashTable;
118:
119: 120: 121: 122: 123:
124: private $_drawingHashTable;
125:
126: 127: 128: 129: 130:
131: public function __construct(PHPExcel $pPHPExcel = null)
132: {
133:
134: $this->setPHPExcel($pPHPExcel);
135:
136: $writerPartsArray = array( 'stringtable' => 'PHPExcel_Writer_Excel2007_StringTable',
137: 'contenttypes' => 'PHPExcel_Writer_Excel2007_ContentTypes',
138: 'docprops' => 'PHPExcel_Writer_Excel2007_DocProps',
139: 'rels' => 'PHPExcel_Writer_Excel2007_Rels',
140: 'theme' => 'PHPExcel_Writer_Excel2007_Theme',
141: 'style' => 'PHPExcel_Writer_Excel2007_Style',
142: 'workbook' => 'PHPExcel_Writer_Excel2007_Workbook',
143: 'worksheet' => 'PHPExcel_Writer_Excel2007_Worksheet',
144: 'drawing' => 'PHPExcel_Writer_Excel2007_Drawing',
145: 'comments' => 'PHPExcel_Writer_Excel2007_Comments',
146: 'chart' => 'PHPExcel_Writer_Excel2007_Chart',
147: 'relsvba' => 'PHPExcel_Writer_Excel2007_RelsVBA',
148: 'relsribbonobjects' => 'PHPExcel_Writer_Excel2007_RelsRibbon'
149: );
150:
151:
152:
153: foreach ($writerPartsArray as $writer => $class) {
154: $this->_writerParts[$writer] = new $class($this);
155: }
156:
157: $hashTablesArray = array( '_stylesConditionalHashTable', '_fillHashTable', '_fontHashTable',
158: '_bordersHashTable', '_numFmtHashTable', '_drawingHashTable',
159: '_styleHashTable'
160: );
161:
162:
163: foreach ($hashTablesArray as $tableName) {
164: $this->$tableName = new PHPExcel_HashTable();
165: }
166: }
167:
168: 169: 170: 171: 172: 173:
174: public function getWriterPart($pPartName = '') {
175: if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
176: return $this->_writerParts[strtolower($pPartName)];
177: } else {
178: return null;
179: }
180: }
181:
182: 183: 184: 185: 186: 187:
188: public function save($pFilename = null)
189: {
190: if ($this->_spreadSheet !== NULL) {
191:
192: $this->_spreadSheet->garbageCollect();
193:
194:
195: $originalFilename = $pFilename;
196: if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
197: $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
198: if ($pFilename == '') {
199: $pFilename = $originalFilename;
200: }
201: }
202:
203: $saveDebugLog = PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->getWriteDebugLog();
204: PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->setWriteDebugLog(FALSE);
205: $saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
206: PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
207:
208:
209: $this->_stringTable = array();
210: for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
211: $this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable);
212: }
213:
214:
215: $this->_styleHashTable->addFromSource( $this->getWriterPart('Style')->allStyles($this->_spreadSheet) );
216: $this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) );
217: $this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) );
218: $this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) );
219: $this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_spreadSheet) );
220: $this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet) );
221:
222:
223: $this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet) );
224:
225:
226: $zipClass = PHPExcel_Settings::getZipClass();
227: $objZip = new $zipClass();
228:
229:
230:
231: $ro = new ReflectionObject($objZip);
232: $zipOverWrite = $ro->getConstant('OVERWRITE');
233: $zipCreate = $ro->getConstant('CREATE');
234:
235: if (file_exists($pFilename)) {
236: unlink($pFilename);
237: }
238:
239: if ($objZip->open($pFilename, $zipOverWrite) !== true) {
240: if ($objZip->open($pFilename, $zipCreate) !== true) {
241: throw new PHPExcel_Writer_Exception("Could not open " . $pFilename . " for writing.");
242: }
243: }
244:
245:
246: $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet, $this->_includeCharts));
247:
248:
249: if($this->_spreadSheet->hasMacros()){
250: $macrosCode=$this->_spreadSheet->getMacrosCode();
251: if(!is_null($macrosCode)){
252: $objZip->addFromString('xl/vbaProject.bin', $macrosCode);
253: if($this->_spreadSheet->hasMacrosCertificate()){
254:
255: $objZip->addFromString('xl/vbaProjectSignature.bin', $this->_spreadSheet->getMacrosCertificate());
256: $objZip->addFromString('xl/_rels/vbaProject.bin.rels',
257: $this->getWriterPart('RelsVBA')->writeVBARelationships($this->_spreadSheet));
258: }
259: }
260: }
261:
262: if($this->_spreadSheet->hasRibbon()){
263: $tmpRibbonTarget=$this->_spreadSheet->getRibbonXMLData('target');
264: $objZip->addFromString($tmpRibbonTarget, $this->_spreadSheet->getRibbonXMLData('data'));
265: if($this->_spreadSheet->hasRibbonBinObjects()){
266: $tmpRootPath=dirname($tmpRibbonTarget).'/';
267: $ribbonBinObjects=$this->_spreadSheet->getRibbonBinObjects('data');
268: foreach($ribbonBinObjects as $aPath=>$aContent){
269: $objZip->addFromString($tmpRootPath.$aPath, $aContent);
270: }
271:
272: $objZip->addFromString($tmpRootPath.'_rels/'.basename($tmpRibbonTarget).'.rels',
273: $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->_spreadSheet));
274: }
275: }
276:
277:
278: $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
279: $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
280:
281:
282: $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
283: $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
284: $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_spreadSheet);
285: if ($customPropertiesPart !== NULL) {
286: $objZip->addFromString('docProps/custom.xml', $customPropertiesPart);
287: }
288:
289:
290: $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
291:
292:
293: $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
294:
295:
296: $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
297:
298:
299: $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet, $this->_preCalculateFormulas));
300:
301: $chartCount = 0;
302:
303: for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
304: $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable, $this->_includeCharts));
305: if ($this->_includeCharts) {
306: $charts = $this->_spreadSheet->getSheet($i)->getChartCollection();
307: if (count($charts) > 0) {
308: foreach($charts as $chart) {
309: $objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart));
310: $chartCount++;
311: }
312: }
313: }
314: }
315:
316: $chartRef1 = $chartRef2 = 0;
317:
318: for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
319:
320:
321: $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1), $this->_includeCharts));
322:
323: $drawings = $this->_spreadSheet->getSheet($i)->getDrawingCollection();
324: $drawingCount = count($drawings);
325: if ($this->_includeCharts) {
326: $chartCount = $this->_spreadSheet->getSheet($i)->getChartCount();
327: }
328:
329:
330: if (($drawingCount > 0) || ($chartCount > 0)) {
331:
332: $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i),$chartRef1, $this->_includeCharts));
333:
334:
335: $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i),$chartRef2,$this->_includeCharts));
336: }
337:
338:
339: if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
340:
341: $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
342:
343:
344: $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
345: }
346:
347:
348: if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
349:
350: $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
351:
352:
353: $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
354:
355:
356: foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
357: $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
358: }
359: }
360: }
361:
362:
363: for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
364: if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
365: $imageContents = null;
366: $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
367: if (strpos($imagePath, 'zip://') !== false) {
368: $imagePath = substr($imagePath, 6);
369: $imagePathSplitted = explode('#', $imagePath);
370:
371: $imageZip = new ZipArchive();
372: $imageZip->open($imagePathSplitted[0]);
373: $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
374: $imageZip->close();
375: unset($imageZip);
376: } else {
377: $imageContents = file_get_contents($imagePath);
378: }
379:
380: $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
381: } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
382: ob_start();
383: call_user_func(
384: $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
385: $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
386: );
387: $imageContents = ob_get_contents();
388: ob_end_clean();
389:
390: $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
391: }
392: }
393:
394: PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
395: PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
396:
397:
398: if ($objZip->close() === false) {
399: throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
400: }
401:
402:
403: if ($originalFilename != $pFilename) {
404: if (copy($pFilename, $originalFilename) === false) {
405: throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
406: }
407: @unlink($pFilename);
408: }
409: } else {
410: throw new PHPExcel_Writer_Exception("PHPExcel object unassigned.");
411: }
412: }
413:
414: 415: 416: 417: 418: 419:
420: public function getPHPExcel() {
421: if ($this->_spreadSheet !== null) {
422: return $this->_spreadSheet;
423: } else {
424: throw new PHPExcel_Writer_Exception("No PHPExcel assigned.");
425: }
426: }
427:
428: 429: 430: 431: 432: 433: 434:
435: public function setPHPExcel(PHPExcel $pPHPExcel = null) {
436: $this->_spreadSheet = $pPHPExcel;
437: return $this;
438: }
439:
440: 441: 442: 443: 444:
445: public function getStringTable() {
446: return $this->_stringTable;
447: }
448:
449: 450: 451: 452: 453:
454: public function getStyleHashTable() {
455: return $this->_styleHashTable;
456: }
457:
458: 459: 460: 461: 462:
463: public function getStylesConditionalHashTable() {
464: return $this->_stylesConditionalHashTable;
465: }
466:
467: 468: 469: 470: 471:
472: public function getFillHashTable() {
473: return $this->_fillHashTable;
474: }
475:
476: 477: 478: 479: 480:
481: public function getFontHashTable() {
482: return $this->_fontHashTable;
483: }
484:
485: 486: 487: 488: 489:
490: public function getBordersHashTable() {
491: return $this->_bordersHashTable;
492: }
493:
494: 495: 496: 497: 498:
499: public function getNumFmtHashTable() {
500: return $this->_numFmtHashTable;
501: }
502:
503: 504: 505: 506: 507:
508: public function getDrawingHashTable() {
509: return $this->_drawingHashTable;
510: }
511:
512: 513: 514: 515: 516:
517: public function getOffice2003Compatibility() {
518: return $this->_office2003compatibility;
519: }
520:
521: 522: 523: 524: 525: 526:
527: public function setOffice2003Compatibility($pValue = false) {
528: $this->_office2003compatibility = $pValue;
529: return $this;
530: }
531:
532: }
533: