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); } // XML header $objWriter->startDocument('1.0','UTF-8','yes'); // Types $objWriter->startElement('Types'); $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); // Theme $this->_writeOverrideContentType( $objWriter, '/xl/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml' ); // Styles $this->_writeOverrideContentType( $objWriter, '/xl/styles.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml' ); // Rels $this->_writeDefaultContentType( $objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml' ); // XML $this->_writeDefaultContentType( $objWriter, 'xml', 'application/xml' ); // VML $this->_writeDefaultContentType( $objWriter, 'vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing' ); // Workbook $this->_writeOverrideContentType( $objWriter, '/xl/workbook.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml' ); // DocProps $this->_writeOverrideContentType( $objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml' ); $this->_writeOverrideContentType( $objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml' ); $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties(); if (count($customPropertyList) > 0) { $this->_writeOverrideContentType( $objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml' ); } // Worksheets $sheetCount = $pPHPExcel->getSheetCount(); for ($i = 0; $i < $sheetCount; ++$i) { $this->_writeOverrideContentType( $objWriter, '/xl/worksheets/sheet' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml' ); } // Shared strings $this->_writeOverrideContentType( $objWriter, '/xl/sharedStrings.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml' ); // Add worksheet relationship content types for ($i = 0; $i < $sheetCount; ++$i) { if ($pPHPExcel->getSheet($i)->getDrawingCollection()->count() > 0) { $this->_writeOverrideContentType( $objWriter, '/xl/drawings/drawing' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.drawing+xml' ); } } // Comments for ($i = 0; $i < $sheetCount; ++$i) { if (count($pPHPExcel->getSheet($i)->getComments()) > 0) { $this->_writeOverrideContentType( $objWriter, '/xl/comments' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml' ); } } // Add media content-types $aMediaContentTypes = array(); $mediaCount = $this->getParentWriter()->getDrawingHashTable()->count(); for ($i = 0; $i < $mediaCount; ++$i) { $extension = ''; $mimeType = ''; if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) { $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension()); $mimeType = $this->_getImageMimeType( $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath() ); } else if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) { $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType()); $extension = explode('/', $extension); $extension = $extension[1]; $mimeType = $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType(); } if (!isset( $aMediaContentTypes[$extension]) ) { $aMediaContentTypes[$extension] = $mimeType; $this->_writeDefaultContentType( $objWriter, $extension, $mimeType ); } } $sheetCount = $pPHPExcel->getSheetCount(); for ($i = 0; $i < $sheetCount; ++$i) { if (count($pPHPExcel->getSheet()->getHeaderFooter()->getImages()) > 0) { foreach ($pPHPExcel->getSheet()->getHeaderFooter()->getImages() as $image) { if (!isset( $aMediaContentTypes[strtolower($image->getExtension())]) ) { $aMediaContentTypes[strtolower($image->getExtension())] = $this->_getImageMimeType( $image->getPath() ); $this->_writeDefaultContentType( $objWriter, strtolower($image->getExtension()), $aMediaContentTypes[strtolower($image->getExtension())] ); } } } } $objWriter->endElement(); // Return return $objWriter->getData(); } /** * Get image mime type * * @param string $pFile Filename * @return string Mime Type * @throws Exception */ private function _getImageMimeType($pFile = '') { if (PHPExcel_Shared_File::file_exists($pFile)) { $image = getimagesize($pFile); return image_type_to_mime_type($image[2]); } else { throw new Exception("File $pFile does not exist"); } } /** * Write Default content type * * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer * @param string $pPartname Part name * @param string $pContentType Content type * @throws Exception */ private function _writeDefaultContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '') { if ($pPartname != '' && $pContentType != '') { // Write content type $objWriter->startElement('Default'); $objWriter->writeAttribute('Extension', $pPartname); $objWriter->writeAttribute('ContentType', $pContentType); $objWriter->endElement(); } else { throw new Exception("Invalid parameters passed."); } } /** * Write Override content type * * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer * @param string $pPartname Part name * @param string $pContentType Content type * @throws Exception */ private function _writeOverrideContentType(PHPExcel_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '') { if ($pPartname != '' && $pContentType != '') { // Write content type $objWriter->startElement('Override'); $objWriter->writeAttribute('PartName', $pPartname); $objWriter->writeAttribute('ContentType', $pContentType); $objWriter->endElement(); } else { throw new Exception("Invalid parameters passed."); } } }