30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
38 require_once PHPEXCEL_ROOT .
'PHPExcel.php';
41 require_once PHPEXCEL_ROOT .
'PHPExcel/HashTable.php';
44 require_once PHPEXCEL_ROOT .
'PHPExcel/IComparable.php';
47 require_once PHPEXCEL_ROOT .
'PHPExcel/Worksheet.php';
50 require_once PHPEXCEL_ROOT .
'PHPExcel/Cell.php';
53 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/IWriter.php';
56 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/XMLWriter.php';
59 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/WriterPart.php';
62 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/StringTable.php';
65 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/ContentTypes.php';
68 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/DocProps.php';
71 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/Rels.php';
74 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/Theme.php';
77 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/Style.php';
80 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/Workbook.php';
83 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/Worksheet.php';
86 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/Drawing.php';
89 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/Excel2007/Comments.php';
203 $this->_diskCachingDirectory =
'./';
218 foreach ($this->_writerParts as $writer) {
219 $writer->setParentWriter($this);
223 $this->_stringTable = array();
239 if ($pPartName !=
'' && isset($this->_writerParts[strtolower($pPartName)])) {
240 return $this->_writerParts[strtolower($pPartName)];
252 public function save($pFilename = null)
254 if (!is_null($this->_spreadSheet)) {
256 $this->_spreadSheet->garbageCollect();
259 $originalFilename = $pFilename;
260 if (strtolower($pFilename) ==
'php://output' || strtolower($pFilename) ==
'php://stdout') {
261 $pFilename = @tempnam(
'./',
'phpxltmp');
262 if ($pFilename ==
'') {
263 $pFilename = $originalFilename;
271 $this->_stringTable = array();
272 for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
277 $this->_stylesConditionalHashTable->addFromSource( $this->
getWriterPart(
'Style')->allConditionalStyles($this->_spreadSheet) );
278 $this->_fillHashTable->addFromSource( $this->
getWriterPart(
'Style')->allFills($this->_spreadSheet) );
279 $this->_fontHashTable->addFromSource( $this->
getWriterPart(
'Style')->allFonts($this->_spreadSheet) );
280 $this->_bordersHashTable->addFromSource( $this->
getWriterPart(
'Style')->allBorders($this->_spreadSheet) );
281 $this->_numFmtHashTable->addFromSource( $this->
getWriterPart(
'Style')->allNumberFormats($this->_spreadSheet) );
284 $this->_drawingHashTable->addFromSource( $this->
getWriterPart(
'Drawing')->allDrawings($this->_spreadSheet) );
287 $objZip =
new ZipArchive();
290 if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !==
true) {
291 if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !==
true) {
292 throw new Exception(
"Could not open " . $pFilename .
" for writing.");
297 $objZip->addFromString(
'[Content_Types].xml', $this->
getWriterPart(
'ContentTypes')->writeContentTypes($this->_spreadSheet));
300 $objZip->addFromString(
'_rels/.rels', $this->
getWriterPart(
'Rels')->writeRelationships($this->_spreadSheet));
301 $objZip->addFromString(
'xl/_rels/workbook.xml.rels', $this->
getWriterPart(
'Rels')->writeWorkbookRelationships($this->_spreadSheet));
304 $objZip->addFromString(
'docProps/app.xml', $this->
getWriterPart(
'DocProps')->writeDocPropsApp($this->_spreadSheet));
305 $objZip->addFromString(
'docProps/core.xml', $this->
getWriterPart(
'DocProps')->writeDocPropsCore($this->_spreadSheet));
308 $objZip->addFromString(
'xl/theme/theme1.xml', $this->
getWriterPart(
'Theme')->writeTheme($this->_spreadSheet));
311 $objZip->addFromString(
'xl/sharedStrings.xml', $this->
getWriterPart(
'StringTable')->writeStringTable($this->_stringTable));
314 $objZip->addFromString(
'xl/styles.xml', $this->
getWriterPart(
'Style')->writeStyles($this->_spreadSheet));
317 $objZip->addFromString(
'xl/workbook.xml', $this->
getWriterPart(
'Workbook')->writeWorkbook($this->_spreadSheet));
320 for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
321 $objZip->addFromString(
'xl/worksheets/sheet' . ($i + 1) .
'.xml', $this->
getWriterPart(
'Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i),
$this->_stringTable));
325 for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
328 $objZip->addFromString(
'xl/worksheets/_rels/sheet' . ($i + 1) .
'.xml.rels', $this->
getWriterPart(
'Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1)));
331 if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->count() > 0) {
333 $objZip->addFromString(
'xl/drawings/_rels/drawing' . ($i + 1) .
'.xml.rels', $this->
getWriterPart(
'Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i)));
336 $objZip->addFromString(
'xl/drawings/drawing' . ($i + 1) .
'.xml', $this->
getWriterPart(
'Drawing')->writeDrawings($this->_spreadSheet->getSheet($i)));
340 if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
342 $objZip->addFromString(
'xl/drawings/vmlDrawing' . ($i + 1) .
'.vml', $this->
getWriterPart(
'Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
345 $objZip->addFromString(
'xl/comments' . ($i + 1) .
'.xml', $this->
getWriterPart(
'Comments')->writeComments($this->_spreadSheet->getSheet($i)));
349 if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
351 $objZip->addFromString(
'xl/drawings/vmlDrawingHF' . ($i + 1) .
'.vml', $this->
getWriterPart(
'Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
354 $objZip->addFromString(
'xl/drawings/_rels/vmlDrawingHF' . ($i + 1) .
'.vml.rels', $this->
getWriterPart(
'Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
357 foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
358 $objZip->addFromString(
'xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
366 $imageContents = null;
369 if (strpos($imagePath,
'zip://') !==
false) {
370 $imagePath = substr($imagePath, 6);
371 $imagePathSplitted = explode(
'#', $imagePath);
373 $imageZip =
new ZipArchive();
374 $imageZip->open($imagePathSplitted[0]);
375 $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
379 $imageContents = file_get_contents($imagePath);
382 $objZip->addFromString(
'xl/media/' . str_replace(
' ',
'_', $this->
getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
389 $imageContents = ob_get_contents();
392 $objZip->addFromString(
'xl/media/' . str_replace(
' ',
'_', $this->
getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
399 if ($objZip->close() ===
false) {
400 throw new Exception(
"Could not close zip file $pFilename.");
404 if ($originalFilename != $pFilename) {
405 if (copy($pFilename, $originalFilename) ===
false) {
406 throw new Exception(
"Could not copy temporary zip file $pFilename to $originalFilename.");
411 throw new Exception(
"PHPExcel object unassigned.");
422 if (!is_null($this->_spreadSheet)) {
425 throw new Exception(
"No PHPExcel assigned.");
437 $this->_spreadSheet = $pPHPExcel;
519 $this->_preCalculateFormulas = $pValue;
538 $this->_office2003compatibility = $pValue;
560 $this->_useDiskCaching = $pValue;
562 if (!is_null($pDirectory)) {
563 if (is_dir($pDirectory)) {
564 $this->_diskCachingDirectory = $pDirectory;
566 throw new Exception(
"Directory does not exist: $pDirectory");