From 388d2c80d1e9a71d66a0116ccee6d117afacbd94 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 15 Sep 2010 21:55:37 +0000 Subject: [PATCH] Feature: Support for Extended and User-defined Workbook Properties in Excel2007 Writer git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@61018 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Writer/Excel2007.php | 4 + .../Writer/Excel2007/ContentTypes.php | 7 ++ .../PHPExcel/Writer/Excel2007/DocProps.php | 110 ++++++++++++++---- Classes/PHPExcel/Writer/Excel2007/Rels.php | 12 ++ .../Functionality Cross-Reference.xls | Bin 29696 -> 29696 bytes changelog.txt | 2 +- 6 files changed, 114 insertions(+), 21 deletions(-) diff --git a/Classes/PHPExcel/Writer/Excel2007.php b/Classes/PHPExcel/Writer/Excel2007.php index 33f424eb..610d28d5 100644 --- a/Classes/PHPExcel/Writer/Excel2007.php +++ b/Classes/PHPExcel/Writer/Excel2007.php @@ -242,6 +242,10 @@ class PHPExcel_Writer_Excel2007 implements PHPExcel_Writer_IWriter // 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 (!is_null($customPropertiesPart)) { + $objZip->addFromString('docProps/custom.xml', $customPropertiesPart); + } // Add theme to ZIP file $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet)); diff --git a/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php b/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php index b586076d..d822538d 100644 --- a/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php +++ b/Classes/PHPExcel/Writer/Excel2007/ContentTypes.php @@ -98,6 +98,13 @@ class PHPExcel_Writer_Excel2007_ContentTypes extends PHPExcel_Writer_Excel2007_W $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) { diff --git a/Classes/PHPExcel/Writer/Excel2007/DocProps.php b/Classes/PHPExcel/Writer/Excel2007/DocProps.php index 2fc32bff..249d5575 100644 --- a/Classes/PHPExcel/Writer/Excel2007/DocProps.php +++ b/Classes/PHPExcel/Writer/Excel2007/DocProps.php @@ -57,8 +57,8 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write // Properties $objWriter->startElement('Properties'); - $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties'); - $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes'); + $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties'); + $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes'); // Application $objWriter->writeElement('Application', 'Microsoft Excel'); @@ -74,9 +74,8 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write // Vector $objWriter->startElement('vt:vector'); - $objWriter->writeAttribute('size', '2'); - $objWriter->writeAttribute('baseType', 'variant'); - + $objWriter->writeAttribute('size', '2'); + $objWriter->writeAttribute('baseType', 'variant'); // Variant $objWriter->startElement('vt:variant'); @@ -97,13 +96,13 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write // Vector $objWriter->startElement('vt:vector'); - $objWriter->writeAttribute('size', $pPHPExcel->getSheetCount()); - $objWriter->writeAttribute('baseType', 'lpstr'); + $objWriter->writeAttribute('size', $pPHPExcel->getSheetCount()); + $objWriter->writeAttribute('baseType', 'lpstr'); - $sheetCount = $pPHPExcel->getSheetCount(); - for ($i = 0; $i < $sheetCount; ++$i) { - $objWriter->writeElement('vt:lpstr', $pPHPExcel->getSheet($i)->getTitle()); - } + $sheetCount = $pPHPExcel->getSheetCount(); + for ($i = 0; $i < $sheetCount; ++$i) { + $objWriter->writeElement('vt:lpstr', $pPHPExcel->getSheet($i)->getTitle()); + } $objWriter->endElement(); @@ -155,11 +154,11 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write // cp:coreProperties $objWriter->startElement('cp:coreProperties'); - $objWriter->writeAttribute('xmlns:cp', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties'); - $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); - $objWriter->writeAttribute('xmlns:dcterms', 'http://purl.org/dc/terms/'); - $objWriter->writeAttribute('xmlns:dcmitype', 'http://purl.org/dc/dcmitype/'); - $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); + $objWriter->writeAttribute('xmlns:cp', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties'); + $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); + $objWriter->writeAttribute('xmlns:dcterms', 'http://purl.org/dc/terms/'); + $objWriter->writeAttribute('xmlns:dcmitype', 'http://purl.org/dc/dcmitype/'); + $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); // dc:creator $objWriter->writeElement('dc:creator', $pPHPExcel->getProperties()->getCreator()); @@ -169,14 +168,14 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write // dcterms:created $objWriter->startElement('dcterms:created'); - $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); - $objWriter->writeRaw(date(DATE_W3C, $pPHPExcel->getProperties()->getCreated())); + $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); + $objWriter->writeRaw(date(DATE_W3C, $pPHPExcel->getProperties()->getCreated())); $objWriter->endElement(); // dcterms:modified $objWriter->startElement('dcterms:modified'); - $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); - $objWriter->writeRaw(date(DATE_W3C, $pPHPExcel->getProperties()->getModified())); + $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); + $objWriter->writeRaw(date(DATE_W3C, $pPHPExcel->getProperties()->getModified())); $objWriter->endElement(); // dc:title @@ -199,4 +198,75 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write // Return return $objWriter->getData(); } + + /** + * Write docProps/custom.xml to XML format + * + * @param PHPExcel $pPHPExcel + * @return string XML Output + * @throws Exception + */ + public function writeDocPropsCustom(PHPExcel $pPHPExcel = null) + { + $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties(); + if (count($customPropertyList) == 0) { + return; + } + + // 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); + } + + // XML header + $objWriter->startDocument('1.0','UTF-8','yes'); + + // cp:coreProperties + $objWriter->startElement('Properties'); + $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties'); + $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes'); + + + foreach($customPropertyList as $key => $customProperty) { + $propertyValue = $pPHPExcel->getProperties()->getCustomPropertyValue($customProperty); + $propertyType = $pPHPExcel->getProperties()->getCustomPropertyType($customProperty); + + $objWriter->startElement('property'); + $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}'); + $objWriter->writeAttribute('pid', $key+2); + $objWriter->writeAttribute('name', $customProperty); + + switch($propertyType) { + case 'i' : + $objWriter->writeElement('vt:i4', $propertyValue); + break; + case 'f' : + $objWriter->writeElement('vt:r8', $propertyValue); + break; + case 'b' : + $objWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false'); + break; + case 'd' : + $objWriter->startElement('vt:filetime'); + $objWriter->writeRaw(date(DATE_W3C, $propertyValue)); + $objWriter->endElement(); + break; + default : + $objWriter->writeElement('vt:lpwstr', $propertyValue); + break; + } + + $objWriter->endElement(); + } + + + $objWriter->endElement(); + + // Return + return $objWriter->getData(); + } + } diff --git a/Classes/PHPExcel/Writer/Excel2007/Rels.php b/Classes/PHPExcel/Writer/Excel2007/Rels.php index 2af7d23e..d25527dd 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Rels.php +++ b/Classes/PHPExcel/Writer/Excel2007/Rels.php @@ -59,6 +59,18 @@ class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPar $objWriter->startElement('Relationships'); $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); + $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties(); + if (count($customPropertyList) > 0) { + // Relationship docProps/app.xml + $this->_writeRelationship( + $objWriter, + 4, + 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties', + 'docProps/custom.xml' + ); + + } + // Relationship docProps/app.xml $this->_writeRelationship( $objWriter, diff --git a/Documentation/Functionality Cross-Reference.xls b/Documentation/Functionality Cross-Reference.xls index e3cbb916d0aa20a7bdb3557348dace46eb67409a..a451a6fd5395618318f26c8f358b35bbb799c0ac 100644 GIT binary patch delta 305 zcmZp8!PxMEaYGIZ>xCc8?Fy3%S=t$WHveS#WFV}Q$N&Uv48a+xsU=3O415fGCjT=H zWt_M<#B4e@=gV*g2FW-EhQiHX9M|$Nx@=zL7tPJ+GWlQRUP&$nF$N9>S0LnM_{#v| zaWS|s7%(tQJ`uGLC@YR4D-M?}iw4O`pvX$VWmRH8vXUsWl5p8eF|QdXP2Lx4$i9mK z3^#v^oz0=i2J{0HgDe9-12f1w3=E7A3M|CL4%7}3VrO8Vyeln@(QdP1dOsuMxy>6h cbQ2SUECW9SGst-i z42%#8EW{+rzy%f(We}bGDlLtXZ*yRJKO^J3%?~r=nI{W48*Iusz{J^b#Y=%J^fV*$ LWJYK6Mft1%La{+Q diff --git a/changelog.txt b/changelog.txt index 873041b7..c1674205 100644 --- a/changelog.txt +++ b/changelog.txt @@ -28,7 +28,7 @@ Fixed in SVN: - Feature: (MBaker) Work item 8769 - Implement Gnumeric File Format Initial work on Gnumeric Reader (Worksheet Data, Document Properties and basic Formatting) - Feature: (MBaker) Support for Extended Workbook Properties in Excel2007, Excel5 and OOCalc Readers; support for User-defined Workbook Properties in Excel2007 and OOCalc Readers -- Feature: (MBaker) Support for Extended Workbook Properties in Excel2007 Writer +- Feature: (MBaker) Support for Extended and User-defined Workbook Properties in Excel2007 Writer - Feature: (MBaker) Provided a setGenerateSheetNavigationBlock(false); option to suppress generation of the sheet navigation block when writing multiple worksheets to HTML - Bugfix: (Progi1984) Workitem 7895 - Excel5 : Formula : Percent - Bugfix: (MB) Work item 14143 - NA() doesn't propagate in matrix calc - quick fix in JAMA/Matrix.php