139 $writerPartsArray = array(
'stringtable' =>
'PHPExcel_Writer_Excel2007_StringTable',
140 'contenttypes' =>
'PHPExcel_Writer_Excel2007_ContentTypes',
141 'docprops' =>
'PHPExcel_Writer_Excel2007_DocProps',
142 'rels' =>
'PHPExcel_Writer_Excel2007_Rels',
143 'theme' =>
'PHPExcel_Writer_Excel2007_Theme',
144 'style' =>
'PHPExcel_Writer_Excel2007_Style',
145 'workbook' =>
'PHPExcel_Writer_Excel2007_Workbook',
146 'worksheet' =>
'PHPExcel_Writer_Excel2007_Worksheet',
147 'drawing' =>
'PHPExcel_Writer_Excel2007_Drawing',
148 'comments' =>
'PHPExcel_Writer_Excel2007_Comments'
153 foreach ($writerPartsArray as $writer => $class) {
154 $this->_writerParts[$writer] =
new $class($this);
157 $hashTablesArray = array(
'_stylesConditionalHashTable',
'_fillHashTable',
'_fontHashTable',
158 '_bordersHashTable',
'_numFmtHashTable',
'_drawingHashTable'
162 foreach ($hashTablesArray as $tableName) {
174 if ($pPartName !=
'' && isset($this->_writerParts[strtolower($pPartName)])) {
175 return $this->_writerParts[strtolower($pPartName)];
187 public function save($pFilename = null)
189 if ($this->_spreadSheet !== NULL) {
191 $this->_spreadSheet->garbageCollect();
194 $originalFilename = $pFilename;
195 if (strtolower($pFilename) ==
'php://output' || strtolower($pFilename) ==
'php://stdout') {
196 $pFilename = @tempnam(
'./',
'phpxltmp');
197 if ($pFilename ==
'') {
198 $pFilename = $originalFilename;
208 $this->_stringTable = array();
209 for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
214 $this->_stylesConditionalHashTable->addFromSource( $this->
getWriterPart(
'Style')->allConditionalStyles($this->_spreadSheet) );
215 $this->_fillHashTable->addFromSource( $this->
getWriterPart(
'Style')->allFills($this->_spreadSheet) );
216 $this->_fontHashTable->addFromSource( $this->
getWriterPart(
'Style')->allFonts($this->_spreadSheet) );
217 $this->_bordersHashTable->addFromSource( $this->
getWriterPart(
'Style')->allBorders($this->_spreadSheet) );
218 $this->_numFmtHashTable->addFromSource( $this->
getWriterPart(
'Style')->allNumberFormats($this->_spreadSheet) );
221 $this->_drawingHashTable->addFromSource( $this->
getWriterPart(
'Drawing')->allDrawings($this->_spreadSheet) );
225 $objZip =
new $zipClass();
227 if (file_exists($pFilename)) {
231 if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !==
true) {
232 if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !==
true) {
233 throw new Exception(
"Could not open " . $pFilename .
" for writing.");
238 $objZip->addFromString(
'[Content_Types].xml', $this->
getWriterPart(
'ContentTypes')->writeContentTypes($this->_spreadSheet));
241 $objZip->addFromString(
'_rels/.rels', $this->
getWriterPart(
'Rels')->writeRelationships($this->_spreadSheet));
242 $objZip->addFromString(
'xl/_rels/workbook.xml.rels', $this->
getWriterPart(
'Rels')->writeWorkbookRelationships($this->_spreadSheet));
245 $objZip->addFromString(
'docProps/app.xml', $this->
getWriterPart(
'DocProps')->writeDocPropsApp($this->_spreadSheet));
246 $objZip->addFromString(
'docProps/core.xml', $this->
getWriterPart(
'DocProps')->writeDocPropsCore($this->_spreadSheet));
247 $customPropertiesPart = $this->
getWriterPart(
'DocProps')->writeDocPropsCustom($this->_spreadSheet);
248 if ($customPropertiesPart !== NULL) {
249 $objZip->addFromString(
'docProps/custom.xml', $customPropertiesPart);
253 $objZip->addFromString(
'xl/theme/theme1.xml', $this->
getWriterPart(
'Theme')->writeTheme($this->_spreadSheet));
256 $objZip->addFromString(
'xl/sharedStrings.xml', $this->
getWriterPart(
'StringTable')->writeStringTable($this->_stringTable));
259 $objZip->addFromString(
'xl/styles.xml', $this->
getWriterPart(
'Style')->writeStyles($this->_spreadSheet));
262 $objZip->addFromString(
'xl/workbook.xml', $this->
getWriterPart(
'Workbook')->writeWorkbook($this->_spreadSheet));
265 for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
266 $objZip->addFromString(
'xl/worksheets/sheet' . ($i + 1) .
'.xml', $this->
getWriterPart(
'Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i),
$this->_stringTable));
270 for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
273 $objZip->addFromString(
'xl/worksheets/_rels/sheet' . ($i + 1) .
'.xml.rels', $this->
getWriterPart(
'Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1)));
276 if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->count() > 0) {
278 $objZip->addFromString(
'xl/drawings/_rels/drawing' . ($i + 1) .
'.xml.rels', $this->
getWriterPart(
'Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i)));
281 $objZip->addFromString(
'xl/drawings/drawing' . ($i + 1) .
'.xml', $this->
getWriterPart(
'Drawing')->writeDrawings($this->_spreadSheet->getSheet($i)));
285 if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
287 $objZip->addFromString(
'xl/drawings/vmlDrawing' . ($i + 1) .
'.vml', $this->
getWriterPart(
'Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
290 $objZip->addFromString(
'xl/comments' . ($i + 1) .
'.xml', $this->
getWriterPart(
'Comments')->writeComments($this->_spreadSheet->getSheet($i)));
294 if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
296 $objZip->addFromString(
'xl/drawings/vmlDrawingHF' . ($i + 1) .
'.vml', $this->
getWriterPart(
'Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
299 $objZip->addFromString(
'xl/drawings/_rels/vmlDrawingHF' . ($i + 1) .
'.vml.rels', $this->
getWriterPart(
'Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
302 foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
303 $objZip->addFromString(
'xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
311 $imageContents = null;
314 if (strpos($imagePath,
'zip://') !==
false) {
315 $imagePath = substr($imagePath, 6);
316 $imagePathSplitted = explode(
'#', $imagePath);
318 $imageZip =
new ZipArchive();
319 $imageZip->open($imagePathSplitted[0]);
320 $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
324 $imageContents = file_get_contents($imagePath);
327 $objZip->addFromString(
'xl/media/' . str_replace(
' ',
'_', $this->
getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
334 $imageContents = ob_get_contents();
337 $objZip->addFromString(
'xl/media/' . str_replace(
' ',
'_', $this->
getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
345 if ($objZip->close() ===
false) {
346 throw new Exception(
"Could not close zip file $pFilename.");
350 if ($originalFilename != $pFilename) {
351 if (copy($pFilename, $originalFilename) ===
false) {
352 throw new Exception(
"Could not copy temporary zip file $pFilename to $originalFilename.");
357 throw new Exception(
"PHPExcel object unassigned.");
368 if ($this->_spreadSheet !== null) {
371 throw new Exception(
"No PHPExcel assigned.");
383 $this->_spreadSheet = $pPHPExcel;
465 $this->_preCalculateFormulas = $pValue;
484 $this->_office2003compatibility = $pValue;
506 $this->_useDiskCaching = $pValue;
508 if ($pDirectory !== NULL) {
509 if (is_dir($pDirectory)) {
510 $this->_diskCachingDirectory = $pDirectory;
512 throw new Exception(
"Directory does not exist: $pDirectory");