diff --git a/Classes/PHPExcel/Shared/TempFileTracker.php b/Classes/PHPExcel/Shared/TempFileTracker.php new file mode 100644 index 000000000..6edf9fc9c --- /dev/null +++ b/Classes/PHPExcel/Shared/TempFileTracker.php @@ -0,0 +1,88 @@ +_files = array (); + } + + function __destruct () + { + $this->removeAllFiles (); + } + + /** + * Register a new temporary file. + * + * @param string $filename + */ + public function registerFile ($filename) + { + $this->_files[] = $filename; + } + + /** + * Generate a new temporary file name, and register it. + * + * @param string $prefix the new filename prefix. + * @return string the new temporary filename. + */ + public function getNewFile ($prefix) + { + $filename = @tempnam (PHPExcel_Shared_File::sys_get_temp_dir(), $prefix); + $this->registerFile ($filename); + return $filename; + } + + /** + * Remove all the registered files. + */ + public function removeAllFiles () + { + foreach ($this->_files as $file) + unlink ($file); + $this->_files = array (); + } +} + +?> diff --git a/Classes/PHPExcel/Shared/XMLWriter.php b/Classes/PHPExcel/Shared/XMLWriter.php index beca51fc5..a69f11df7 100644 --- a/Classes/PHPExcel/Shared/XMLWriter.php +++ b/Classes/PHPExcel/Shared/XMLWriter.php @@ -42,36 +42,33 @@ * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) */ class PHPExcel_Shared_XMLWriter extends XMLWriter { - /** Temporary storage method */ - const STORAGE_MEMORY = 1; - const STORAGE_DISK = 2; + const FLUSH_FREQUENCY = 100; /** * Temporary filename * * @var string */ - private $_tempFileName = ''; + private $_filename = ''; + + private $counter; /** * Create a new PHPExcel_Shared_XMLWriter instance * - * @param int $pTemporaryStorage Temporary storage location - * @param string $pTemporaryStorageFolder Temporary storage folder + * @param string $filename Temporary storage filename (empty to use memory) */ - public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = NULL) { + public function __construct($filename = NULL) { // Open temporary storage - if ($pTemporaryStorage == self::STORAGE_MEMORY) { + if (!$filename) { $this->openMemory(); } else { - // Create temporary filename - if ($pTemporaryStorageFolder === NULL) - $pTemporaryStorageFolder = PHPExcel_Shared_File::sys_get_temp_dir(); - $this->_tempFileName = @tempnam($pTemporaryStorageFolder, 'xml'); + $this->_filename = $filename; // Open storage - if ($this->openUri($this->_tempFileName) === false) { + if ($this->openUri($this->_filename) === false) { // Fallback to memory... + $this->_filename = ''; $this->openMemory(); } } @@ -80,16 +77,44 @@ public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTempora if (DEBUGMODE_ENABLED) { $this->setIndent(true); } + $this->counter = self::FLUSH_FREQUENCY; } - /** - * Destructor - */ - public function __destruct() { - // Unlink temporary files - if ($this->_tempFileName != '') { - @unlink($this->_tempFileName); - } + public function flush($empty=true) + { + parent::flush($empty); + $this->counter = self::FLUSH_FREQUENCY; + } + + private function flushIfNecessary() + { + if ($this->_filename == '') + return; + + --$this->counter; + if ($this->counter <= 0) + $this->flush(); + } + + public function endElement() + { + $ret = parent::endElement(); + $this->flushIfNecessary(); + return $ret; + } + + public function writeElement($name, $content=null) + { + $ret = parent::writeElement($name, $content); + $this->flushIfNecessary(); + return $ret; + } + + public function writeAttribute($name, $value) + { + $ret = parent::writeAttribute($name, $value); + $this->flushIfNecessary(); + return $ret; } /** @@ -98,14 +123,23 @@ public function __destruct() { * @return $data */ public function getData() { - if ($this->_tempFileName == '') { + if ($this->_filename == '') { return $this->outputMemory(true); } else { $this->flush(); - return file_get_contents($this->_tempFileName); + return file_get_contents($this->_filename); } } + /** + * Get the temporary file name. + * + * @return filename; + */ + public function getFileName() { + return $this->_filename; + } + /** * Fallback method for writeRaw, introduced in PHP 5.2 * diff --git a/Classes/PHPExcel/Shared/ZipArchive.php b/Classes/PHPExcel/Shared/ZipArchive.php index 9a801a841..00d120814 100644 --- a/Classes/PHPExcel/Shared/ZipArchive.php +++ b/Classes/PHPExcel/Shared/ZipArchive.php @@ -85,6 +85,30 @@ public function close() { } + /** + * Add a new file to the zip archive. + * + * @param string $filename The path to the file to add to the zip archive + * @param string $localname Directory/Name of the file to add to the zip archive + */ + public function addFile($filename, $localname=null) + { + if (is_null ($localname)) + $localname = $filename; + + $filenameParts = pathinfo($localname); + copy($filename, $this->_tempDir.'/'.$filenameParts["basename"]); + + $res = $this->_zip->add($this->_tempDir.'/'.$filenameParts["basename"], + PCLZIP_OPT_REMOVE_PATH, $this->_tempDir, + PCLZIP_OPT_ADD_PATH, $filenameParts["dirname"] + ); + if ($res == 0) { + throw new PHPExcel_Writer_Exception("Error zipping files : " . $this->_zip->errorInfo(true)); + } + + unlink($this->_tempDir.'/'.$filenameParts["basename"]); + } /** * Add a new file to the zip archive from a string of raw data. diff --git a/Classes/PHPExcel/Writer/Excel2007.php b/Classes/PHPExcel/Writer/Excel2007.php index 4cf14ac1c..fa470408d 100644 --- a/Classes/PHPExcel/Writer/Excel2007.php +++ b/Classes/PHPExcel/Writer/Excel2007.php @@ -123,6 +123,13 @@ class PHPExcel_Writer_Excel2007 extends PHPExcel_Writer_Abstract implements PHPE */ private $_drawingHashTable; + /** + * Private temporary file list. + * + * @var PHPExcel_Shared_TempFileTracker + */ + private $_tempFileTracker; + /** * Create a new PHPExcel_Writer_Excel2007 * @@ -163,6 +170,8 @@ public function __construct(PHPExcel $pPHPExcel = null) foreach ($hashTablesArray as $tableName) { $this->$tableName = new PHPExcel_HashTable(); } + + $this->_tempFileTracker = new PHPExcel_Shared_TempFileTracker(); } /** @@ -243,7 +252,7 @@ public function save($pFilename = null) } // Add [Content_Types].xml to ZIP file - $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet, $this->_includeCharts)); + $this->getWriterPart('ContentTypes')->addContentTypesToZip($this->_spreadSheet, $objZip, '[Content_Types].xml', $this->_includeCharts); //if hasMacros, add the vbaProject.bin file, Certificate file(if exists) if($this->_spreadSheet->hasMacros()){ @@ -253,8 +262,7 @@ public function save($pFilename = null) if($this->_spreadSheet->hasMacrosCertificate()){//signed macros ? // Yes : add the certificate file and the related rels file $objZip->addFromString('xl/vbaProjectSignature.bin', $this->_spreadSheet->getMacrosCertificate()); - $objZip->addFromString('xl/_rels/vbaProject.bin.rels', - $this->getWriterPart('RelsVBA')->writeVBARelationships($this->_spreadSheet)); + $this->getWriterPart('RelsVBA')->addVBARelationshipsToZip($this->_spreadSheet, $objZip, 'xl/_rels/vbaProject.bin.rels'); } } } @@ -269,44 +277,44 @@ public function save($pFilename = null) $objZip->addFromString($tmpRootPath.$aPath, $aContent); } //the rels for files - $objZip->addFromString($tmpRootPath.'_rels/'.basename($tmpRibbonTarget).'.rels', - $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->_spreadSheet)); + $this->getWriterPart('RelsRibbonObjects')->addRibbonRelationshipsToZip($this->_spreadSheet, $objZip, + $tmpRootPath.'_rels/'.basename($tmpRibbonTarget).'.rels'); } } // Add relationships to ZIP file - $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet)); - $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet)); + $this->getWriterPart('Rels')->addRelationshipsToZip($this->_spreadSheet, $objZip, '_rels/.rels'); + $this->getWriterPart('Rels')->addWorkbookRelationshipsToZip($this->_spreadSheet, $objZip, 'xl/_rels/workbook.xml.rels'); // Add document properties to ZIP file - $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet)); - $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet)); - $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_spreadSheet); - if ($customPropertiesPart !== NULL) { - $objZip->addFromString('docProps/custom.xml', $customPropertiesPart); - } + $this->getWriterPart('DocProps')->addDocPropsAppToZip($this->_spreadSheet, $objZip, 'docProps/app.xml'); + $this->getWriterPart('DocPRops')->addDocPropsCoreToZip($this->_spreadSheet, $objZip, 'docProps/core.xml'); + $this->getWriterPart('DocProps')->addDocPropsCustomToZip($this->_spreadSheet, $objZip, 'docProps/custom.xml'); // Add theme to ZIP file - $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet)); + $this->getWriterPart('Theme')->addThemeToZip($this->_spreadSheet, $objZip, 'xl/theme/theme1.xml'); // Add string table to ZIP file - $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable)); + $this->getWriterPart('StringTable')->addStringTableToZip($this->_stringTable, $objZip, 'xl/sharedStrings.xml'); // Add styles to ZIP file - $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet)); + $this->getWriterPart('Style')->addStylesToZip($this->_spreadSheet, $objZip, 'xl/styles.xml'); // Add workbook to ZIP file - $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet, $this->_preCalculateFormulas)); + $this->getWriterPart('Workbook')->addWorkbookToZip($this->_spreadSheet, $objZip, 'xl/workbook.xml', $this->_preCalculateFormulas); $chartCount = 0; // Add worksheets for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) { - $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable, $this->_includeCharts)); + $currentSheet = $this->_spreadSheet->getSheet($i); + + $this->getWriterPart('Worksheet')->addWorksheetToZip($currentSheet, $objZip, 'xl/worksheets/sheet'.($i+1).'.xml', + $this->_stringTable, $this->_includeCharts); if ($this->_includeCharts) { - $charts = $this->_spreadSheet->getSheet($i)->getChartCollection(); + $charts = $currentSheet->getChartCollection(); if (count($charts) > 0) { foreach($charts as $chart) { - $objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart)); + $this->getWriterPart('Chart')->addChartToZip($chart, $objZip, 'xl/charts/chart' . ($chartCount + 1) . '.xml'); $chartCount++; } } @@ -316,53 +324,58 @@ public function save($pFilename = null) $chartRef1 = $chartRef2 = 0; // Add worksheet relationships (drawings, ...) for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) { + $currentSheet = $this->_spreadSheet->getSheet($i); // Add relationships - $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1), $this->_includeCharts)); + $this->getWriterPart('Rels')->addWorksheetRelationshipsToZip($currentSheet, $objZip, + 'xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', + ($i + 1), $this->_includeCharts); - $drawings = $this->_spreadSheet->getSheet($i)->getDrawingCollection(); + $drawings = $currentSheet->getDrawingCollection(); $drawingCount = count($drawings); if ($this->_includeCharts) { - $chartCount = $this->_spreadSheet->getSheet($i)->getChartCount(); + $chartCount = $currentSheet->getChartCount(); } // Add drawing and image relationship parts if (($drawingCount > 0) || ($chartCount > 0)) { // Drawing relationships - $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i),$chartRef1, $this->_includeCharts)); + $this->getWriterPart('Rels')->addDrawingRelationshipsToZip($currentSheet, $chartRef1, $objZip, + 'xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->_includeCharts); // Drawings - $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i),$chartRef2,$this->_includeCharts)); + $this->getWriterPart('Drawing')->addDrawingsToZip($currentSheet, $chartRef2, $objZip, + 'xl/drawings/drawing' . ($i + 1) . '.xml', $this->_includeCharts); } // Add comment relationship parts - if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) { + if (count($currentSheet->getComments()) > 0) { // VML Comments - $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i))); + $this->getWriterPart('Comments')->addVMLCommentsToZip($currentSheet, $objZip, 'xl/drawings/vmlDrawing' . ($i + 1) . '.vml'); // Comments - $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i))); + $this->getWriterPart('Comments')->addCommentsToZip($currentSheet, $objZip, 'xl/comments' . ($i + 1) . '.xml'); } // Add header/footer relationship parts - if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) { + if (count($currentSheet->getHeaderFooter()->getImages()) > 0) { // VML Drawings - $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i))); + $this->getWriterPart('Drawing')->addVMLHeaderFooterImagesToZip($currentSheet, $objZip, 'xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml'); // VML Drawing relationships - $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i))); + $this->getWriterPart('Rels')->addHeaderFooterDrawingRelationshipsToZip($currentSheet, $objZip, 'xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels'); // Media - foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) { - $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath())); + foreach ($currentSheet->getHeaderFooter()->getImages() as $image) { + $objZip->addFile($image->getPath(), 'xl/media/' . $image->getIndexedFilename()); } } } // Add media for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { + $imageName = str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()); if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) { - $imageContents = null; $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath(); if (strpos($imagePath, 'zip://') !== false) { $imagePath = substr($imagePath, 6); @@ -373,11 +386,11 @@ public function save($pFilename = null) $imageContents = $imageZip->getFromName($imagePathSplitted[1]); $imageZip->close(); unset($imageZip); + $objzip->addFromString('xl/media/' . $imagename, $imagecontents); } else { - $imageContents = file_get_contents($imagePath); + $objZip->addFile($imagePath, 'xl/media/' . $imageName); } - $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) { ob_start(); call_user_func( @@ -387,7 +400,7 @@ public function save($pFilename = null) $imageContents = ob_get_contents(); ob_end_clean(); - $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); + $objZip->addFromString('xl/media/' . $imageName, $imageContents); } } @@ -396,12 +409,14 @@ public function save($pFilename = null) // Close file if ($objZip->close() === false) { + $this->_tempFileTracker->removeAllFiles (); throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename."); } // If a temporary file was used, copy it to the correct file stream if ($originalFilename != $pFilename) { if (copy($pFilename, $originalFilename) === false) { + $this->_tempFileTracker->removeAllFiles (); throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename."); } @unlink($pFilename); @@ -409,6 +424,8 @@ public function save($pFilename = null) } else { throw new PHPExcel_Writer_Exception("PHPExcel object unassigned."); } + + $this->_tempFileTracker->removeAllFiles (); } /** @@ -529,4 +546,12 @@ public function setOffice2003Compatibility($pValue = false) { return $this; } + /** + * Get the temporary file tracker. + * + * @return PHPExcel_Shared_TempFileTracker + */ + public function getTempFileTracker() { + return $this->_tempFileTracker; + } } diff --git a/Classes/PHPExcel/Writer/Excel2007/Chart.php b/Classes/PHPExcel/Writer/Excel2007/Chart.php index 0b686b912..59c53f5c0 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Chart.php +++ b/Classes/PHPExcel/Writer/Excel2007/Chart.php @@ -39,18 +39,15 @@ class PHPExcel_Writer_Excel2007_Chart extends PHPExcel_Writer_Excel2007_WriterPa * Write charts to XML format * * @param PHPExcel_Chart $pChart - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeChart(PHPExcel_Chart $pChart = null) + public function addChartToZip(PHPExcel_Chart $pChart, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); + // Ensure that data series values are up-to-date before we save $pChart->refresh(); @@ -111,8 +108,8 @@ public function writeChart(PHPExcel_Chart $pChart = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** diff --git a/Classes/PHPExcel/Writer/Excel2007/Comments.php b/Classes/PHPExcel/Writer/Excel2007/Comments.php index dc809fa8e..555cea731 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Comments.php +++ b/Classes/PHPExcel/Writer/Excel2007/Comments.php @@ -38,19 +38,15 @@ class PHPExcel_Writer_Excel2007_Comments extends PHPExcel_Writer_Excel2007_Write /** * Write comments to XML format * - * @param PHPExcel_Worksheet $pWorksheet - * @return string XML Output + * @param PHPExcel_Worksheet $pWorksheet + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeComments(PHPExcel_Worksheet $pWorksheet = null) + public function addCommentsToZip(PHPExcel_Worksheet $pWorksheet, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -87,8 +83,8 @@ public function writeComments(PHPExcel_Worksheet $pWorksheet = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** @@ -118,19 +114,15 @@ public function _writeComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCel /** * Write VML comments to XML format * - * @param PHPExcel_Worksheet $pWorksheet - * @return string XML Output + * @param PHPExcel_Worksheet $pWorksheet + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeVMLComments(PHPExcel_Worksheet $pWorksheet = null) + public function writeVMLComments(PHPExcel_Worksheet $pWorksheet, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -183,8 +175,8 @@ public function writeVMLComments(PHPExcel_Worksheet $pWorksheet = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** diff --git a/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php b/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php index 557853654..9cc6f5d86 100644 --- a/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php +++ b/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php @@ -39,19 +39,15 @@ class PHPExcel_Writer_Excel2007_ContentTypes extends PHPExcel_Writer_Excel2007_W * Write content types to XML format * * @param PHPExcel $pPHPExcel + * @param ZipArchive $objZip + * @param string $filename * @param boolean $includeCharts Flag indicating if we should include drawing details for charts - * @return string XML Output * @throws PHPExcel_Writer_Exception */ - public function writeContentTypes(PHPExcel $pPHPExcel = null, $includeCharts = FALSE) + public function addContentTypesToZip(PHPExcel $pPHPExcel, $objZip, $filename, $includeCharts = FALSE) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -222,8 +218,8 @@ public function writeContentTypes(PHPExcel $pPHPExcel = null, $includeCharts = F $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** diff --git a/Classes/PHPExcel/Writer/Excel2007/DocProps.php b/Classes/PHPExcel/Writer/Excel2007/DocProps.php index f8821379c..ce52ff88d 100644 --- a/Classes/PHPExcel/Writer/Excel2007/DocProps.php +++ b/Classes/PHPExcel/Writer/Excel2007/DocProps.php @@ -39,18 +39,14 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write * Write docProps/app.xml to XML format * * @param PHPExcel $pPHPExcel - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeDocPropsApp(PHPExcel $pPHPExcel = null) + public function addDocPropsAppToZip(PHPExcel $pPHPExcel, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -128,26 +124,22 @@ public function writeDocPropsApp(PHPExcel $pPHPExcel = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** * Write docProps/core.xml to XML format * * @param PHPExcel $pPHPExcel - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeDocPropsCore(PHPExcel $pPHPExcel = null) + public function addDocPropsCoreToZip(PHPExcel $pPHPExcel, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -195,18 +187,19 @@ public function writeDocPropsCore(PHPExcel $pPHPExcel = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** * Write docProps/custom.xml to XML format * * @param PHPExcel $pPHPExcel - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeDocPropsCustom(PHPExcel $pPHPExcel = null) + public function addDocPropsCustomToZip(PHPExcel $pPHPExcel, $objZip, $filename) { $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties(); if (empty($customPropertyList)) { @@ -214,12 +207,7 @@ public function writeDocPropsCustom(PHPExcel $pPHPExcel = null) } // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -265,8 +253,8 @@ public function writeDocPropsCustom(PHPExcel $pPHPExcel = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } } diff --git a/Classes/PHPExcel/Writer/Excel2007/Drawing.php b/Classes/PHPExcel/Writer/Excel2007/Drawing.php index 1cf971ed4..5afe28498 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Drawing.php +++ b/Classes/PHPExcel/Writer/Excel2007/Drawing.php @@ -40,19 +40,15 @@ class PHPExcel_Writer_Excel2007_Drawing extends PHPExcel_Writer_Excel2007_Writer * * @param PHPExcel_Worksheet $pWorksheet * @param int &$chartRef Chart ID + * @param ZipArchive $objZip + * @param string $filename * @param boolean $includeCharts Flag indicating if we should include drawing details for charts - * @return string XML Output * @throws PHPExcel_Writer_Exception */ - public function writeDrawings(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $includeCharts = FALSE) + public function addDrawingsToZip(PHPExcel_Worksheet $pWorksheet, &$chartRef, $objZip, $filename, $includeCharts = FALSE) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -85,8 +81,8 @@ public function writeDrawings(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** @@ -382,19 +378,15 @@ public function _writeDrawing(PHPExcel_Shared_XMLWriter $objWriter = null, PHPEx /** * Write VML header/footer images to XML format * - * @param PHPExcel_Worksheet $pWorksheet - * @return string XML Output + * @param PHPExcel_Worksheet $pWorksheet + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeVMLHeaderFooterImages(PHPExcel_Worksheet $pWorksheet = null) + public function addVMLHeaderFooterImagesToZip(PHPExcel_Worksheet $pWorksheet, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -522,8 +514,8 @@ public function writeVMLHeaderFooterImages(PHPExcel_Worksheet $pWorksheet = null $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** diff --git a/Classes/PHPExcel/Writer/Excel2007/Rels.php b/Classes/PHPExcel/Writer/Excel2007/Rels.php index a7d36c0a9..036c9d8f2 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Rels.php +++ b/Classes/PHPExcel/Writer/Excel2007/Rels.php @@ -39,18 +39,14 @@ class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPar * Write relationships to XML format * * @param PHPExcel $pPHPExcel - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeRelationships(PHPExcel $pPHPExcel = null) + public function addRelationshipsToZip(PHPExcel $pPHPExcel, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -106,26 +102,22 @@ public function writeRelationships(PHPExcel $pPHPExcel = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** * Write workbook relationships to XML format * * @param PHPExcel $pPHPExcel - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null) + public function addWorkbookRelationshipsToZip(PHPExcel $pPHPExcel, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -182,8 +174,8 @@ public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** @@ -194,20 +186,16 @@ public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null) * rId_hyperlink_x - Hyperlinks * * @param PHPExcel_Worksheet $pWorksheet + * @param ZipArchive $objZip + * @param string $filename * @param int $pWorksheetId * @param boolean $includeCharts Flag indicating if we should write charts - * @return string XML Output * @throws PHPExcel_Writer_Exception */ - public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1, $includeCharts = FALSE) + public function addWorksheetRelationshipsToZip(PHPExcel_Worksheet $pWorksheet, $objZip, $filename, $pWorksheetId = 1, $includeCharts = FALSE) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -295,8 +283,8 @@ public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = nul $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** @@ -304,19 +292,15 @@ public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = nul * * @param PHPExcel_Worksheet $pWorksheet * @param int &$chartRef Chart ID + * @param ZipArchive $objZip + * @param string $filename * @param boolean $includeCharts Flag indicating if we should write charts - * @return string XML Output * @throws PHPExcel_Writer_Exception */ - public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $includeCharts = FALSE) + public function addDrawingRelationshipsToZip(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $objZip, $filename, $includeCharts = FALSE) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -361,26 +345,22 @@ public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null, $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** * Write header/footer drawing relationships to XML format * - * @param PHPExcel_Worksheet $pWorksheet - * @return string XML Output + * @param PHPExcel_Worksheet $pWorksheet + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null) + public function addHeaderFooterDrawingRelationshipsToZip(PHPExcel_Worksheet $pWorksheet, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -402,8 +382,8 @@ public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorks $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** diff --git a/Classes/PHPExcel/Writer/Excel2007/RelsRibbon.php b/Classes/PHPExcel/Writer/Excel2007/RelsRibbon.php index 615f2cbd9..fb55c445d 100644 --- a/Classes/PHPExcel/Writer/Excel2007/RelsRibbon.php +++ b/Classes/PHPExcel/Writer/Excel2007/RelsRibbon.php @@ -39,17 +39,13 @@ class PHPExcel_Writer_Excel2007_RelsRibbon extends PHPExcel_Writer_Excel2007_Wri * Write relationships for additional objects of custom UI (ribbon) * * @param PHPExcel $pPHPExcel - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeRibbonRelationships(PHPExcel $pPHPExcel = null){ + public function addRibbonRelationshipsToZip(PHPExcel $pPHPExcel, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -69,9 +65,8 @@ public function writeRibbonRelationships(PHPExcel $pPHPExcel = null){ } $objWriter->endElement();//Relationships - // Return - return $objWriter->getData(); - + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } } diff --git a/Classes/PHPExcel/Writer/Excel2007/RelsVBA.php b/Classes/PHPExcel/Writer/Excel2007/RelsVBA.php index 3f87d81f2..b3d21e035 100644 --- a/Classes/PHPExcel/Writer/Excel2007/RelsVBA.php +++ b/Classes/PHPExcel/Writer/Excel2007/RelsVBA.php @@ -39,17 +39,13 @@ class PHPExcel_Writer_Excel2007_RelsVBA extends PHPExcel_Writer_Excel2007_Writer * Write relationships for a signed VBA Project * * @param PHPExcel $pPHPExcel - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeVBARelationships(PHPExcel $pPHPExcel = null){ + public function addVBARelationshipsToZip(PHPExcel $pPHPExcel, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -64,9 +60,8 @@ public function writeVBARelationships(PHPExcel $pPHPExcel = null){ $objWriter->endElement();//Relationship $objWriter->endElement();//Relationships - // Return - return $objWriter->getData(); - + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } } diff --git a/Classes/PHPExcel/Writer/Excel2007/StringTable.php b/Classes/PHPExcel/Writer/Excel2007/StringTable.php index e8ca1c5a5..abd4d9ae6 100644 --- a/Classes/PHPExcel/Writer/Excel2007/StringTable.php +++ b/Classes/PHPExcel/Writer/Excel2007/StringTable.php @@ -89,19 +89,15 @@ public function createStringTable($pSheet = null, $pExistingTable = null) * Write string table to XML format * * @param string[] $pStringTable - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeStringTable($pStringTable = null) + public function addStringTableToZip($pStringTable, $objZip, $filename) { if ($pStringTable !== NULL) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -132,8 +128,8 @@ public function writeStringTable($pStringTable = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } else { throw new PHPExcel_Writer_Exception("Invalid string table array passed."); } diff --git a/Classes/PHPExcel/Writer/Excel2007/Style.php b/Classes/PHPExcel/Writer/Excel2007/Style.php index 9857043b8..8ff3c99ae 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Style.php +++ b/Classes/PHPExcel/Writer/Excel2007/Style.php @@ -39,18 +39,14 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa * Write styles to XML format * * @param PHPExcel $pPHPExcel - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeStyles(PHPExcel $pPHPExcel = null) + public function addStylesToZip(PHPExcel $pPHPExcel, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -161,8 +157,8 @@ public function writeStyles(PHPExcel $pPHPExcel = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** diff --git a/Classes/PHPExcel/Writer/Excel2007/Theme.php b/Classes/PHPExcel/Writer/Excel2007/Theme.php index c67b94816..a77e8ce10 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Theme.php +++ b/Classes/PHPExcel/Writer/Excel2007/Theme.php @@ -133,18 +133,14 @@ class PHPExcel_Writer_Excel2007_Theme extends PHPExcel_Writer_Excel2007_WriterPa * Write theme to XML format * * @param PHPExcel $pPHPExcel - * @return string XML Output + * @param ZipArchive $objZip + * @param string $filename * @throws PHPExcel_Writer_Exception */ - public function writeTheme(PHPExcel $pPHPExcel = null) + public function addThemeToZip(PHPExcel $pPHPExcel, $objZip, $filename) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -809,8 +805,8 @@ public function writeTheme(PHPExcel $pPHPExcel = null) $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** diff --git a/Classes/PHPExcel/Writer/Excel2007/Workbook.php b/Classes/PHPExcel/Writer/Excel2007/Workbook.php index f30929476..940c550c6 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Workbook.php +++ b/Classes/PHPExcel/Writer/Excel2007/Workbook.php @@ -36,22 +36,18 @@ class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_WriterPart { /** - * Write workbook to XML format + * Write workbook to XML format into the ZIP file. * * @param PHPExcel $pPHPExcel + * @param ZipArchive $objZip + * @param string $filename * @param boolean $recalcRequired Indicate whether formulas should be recalculated before writing - * @return string XML Output - * @throws PHPExcel_Writer_Exception + * @throws PHPExcel_Writer_Exception */ - public function writeWorkbook(PHPExcel $pPHPExcel = null, $recalcRequired = FALSE) + public function addWorkbookToZip(PHPExcel $pPHPExcel, $objZip, $filename, $recalcRequired = FALSE) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -87,8 +83,8 @@ public function writeWorkbook(PHPExcel $pPHPExcel = null, $recalcRequired = FALS $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } /** diff --git a/Classes/PHPExcel/Writer/Excel2007/Worksheet.php b/Classes/PHPExcel/Writer/Excel2007/Worksheet.php index 760581647..44dae0622 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Worksheet.php +++ b/Classes/PHPExcel/Writer/Excel2007/Worksheet.php @@ -36,24 +36,20 @@ class PHPExcel_Writer_Excel2007_Worksheet extends PHPExcel_Writer_Excel2007_WriterPart { /** - * Write worksheet to XML format + * Write worksheet to XML format into the ZIP file. * * @param PHPExcel_Worksheet $pSheet + * @param ZipArchive $objZip + * @param string $filename * @param string[] $pStringTable * @param boolean $includeCharts Flag indicating if we should write charts - * @return string XML Output * @throws PHPExcel_Writer_Exception */ - public function writeWorksheet($pSheet = null, $pStringTable = null, $includeCharts = FALSE) + public function addWorksheetToZip($pSheet, $objZip, $filename, $pStringTable = null, $includeCharts = FALSE) { if (!is_null($pSheet)) { // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY); - } + $objWriter = $this->createXMLWriter(); // XML header $objWriter->startDocument('1.0','UTF-8','yes'); @@ -129,8 +125,8 @@ public function writeWorksheet($pSheet = null, $pStringTable = null, $includeCha $objWriter->endElement(); - // Return - return $objWriter->getData(); + // Add the generated file to the Zip file. + $this->addXMLToZip($objWriter, $objZip, $filename); } else { throw new PHPExcel_Writer_Exception("Invalid PHPExcel_Worksheet object passed."); } diff --git a/Classes/PHPExcel/Writer/Excel2007/WriterPart.php b/Classes/PHPExcel/Writer/Excel2007/WriterPart.php index 68b1124fd..52e143ac6 100644 --- a/Classes/PHPExcel/Writer/Excel2007/WriterPart.php +++ b/Classes/PHPExcel/Writer/Excel2007/WriterPart.php @@ -78,4 +78,24 @@ public function __construct(PHPExcel_Writer_IWriter $pWriter = null) { } } + protected function createXMLWriter() { + if ($this->getParentWriter()->getUseDiskCaching()) { + $filename = $this->getParentWriter()->getTempFileTracker()->getNewFile('xmlw'); + return new PHPExcel_Shared_XMLWriter($filename); + } else { + return new PHPExcel_Shared_XMLWriter(NULL); + } + } + + protected function addXMLToZip($objWriter, $objZip, $filename) { + // Add the generated file to the Zip file. + $tempFileName = $objWriter->getFileName(); + + if ($tempFileName) { + $objZip->addFile($tempFileName, $filename); + } + else { + $objZip->addFromString($filename, $objWriter->getData()); + } + } }