DocProps.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * PHPExcel_Writer_Excel2007_DocProps
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer_Excel2007
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_WriterPart
  28. {
  29. /**
  30. * Write docProps/app.xml to XML format
  31. *
  32. * @param PHPExcel $pPHPExcel
  33. * @return string XML Output
  34. * @throws PHPExcel_Writer_Exception
  35. */
  36. public function writeDocPropsApp(PHPExcel $pPHPExcel = null)
  37. {
  38. // Create XML writer
  39. $objWriter = null;
  40. if ($this->getParentWriter()->getUseDiskCaching()) {
  41. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  42. } else {
  43. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  44. }
  45. // XML header
  46. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  47. // Properties
  48. $objWriter->startElement('Properties');
  49. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties');
  50. $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
  51. // Application
  52. $objWriter->writeElement('Application', 'Microsoft Excel');
  53. // DocSecurity
  54. $objWriter->writeElement('DocSecurity', '0');
  55. // ScaleCrop
  56. $objWriter->writeElement('ScaleCrop', 'false');
  57. // HeadingPairs
  58. $objWriter->startElement('HeadingPairs');
  59. // Vector
  60. $objWriter->startElement('vt:vector');
  61. $objWriter->writeAttribute('size', '2');
  62. $objWriter->writeAttribute('baseType', 'variant');
  63. // Variant
  64. $objWriter->startElement('vt:variant');
  65. $objWriter->writeElement('vt:lpstr', 'Worksheets');
  66. $objWriter->endElement();
  67. // Variant
  68. $objWriter->startElement('vt:variant');
  69. $objWriter->writeElement('vt:i4', $pPHPExcel->getSheetCount());
  70. $objWriter->endElement();
  71. $objWriter->endElement();
  72. $objWriter->endElement();
  73. // TitlesOfParts
  74. $objWriter->startElement('TitlesOfParts');
  75. // Vector
  76. $objWriter->startElement('vt:vector');
  77. $objWriter->writeAttribute('size', $pPHPExcel->getSheetCount());
  78. $objWriter->writeAttribute('baseType', 'lpstr');
  79. $sheetCount = $pPHPExcel->getSheetCount();
  80. for ($i = 0; $i < $sheetCount; ++$i) {
  81. $objWriter->writeElement('vt:lpstr', $pPHPExcel->getSheet($i)->getTitle());
  82. }
  83. $objWriter->endElement();
  84. $objWriter->endElement();
  85. // Company
  86. $objWriter->writeElement('Company', $pPHPExcel->getProperties()->getCompany());
  87. // Company
  88. $objWriter->writeElement('Manager', $pPHPExcel->getProperties()->getManager());
  89. // LinksUpToDate
  90. $objWriter->writeElement('LinksUpToDate', 'false');
  91. // SharedDoc
  92. $objWriter->writeElement('SharedDoc', 'false');
  93. // HyperlinksChanged
  94. $objWriter->writeElement('HyperlinksChanged', 'false');
  95. // AppVersion
  96. $objWriter->writeElement('AppVersion', '12.0000');
  97. $objWriter->endElement();
  98. // Return
  99. return $objWriter->getData();
  100. }
  101. /**
  102. * Write docProps/core.xml to XML format
  103. *
  104. * @param PHPExcel $pPHPExcel
  105. * @return string XML Output
  106. * @throws PHPExcel_Writer_Exception
  107. */
  108. public function writeDocPropsCore(PHPExcel $pPHPExcel = null)
  109. {
  110. // Create XML writer
  111. $objWriter = null;
  112. if ($this->getParentWriter()->getUseDiskCaching()) {
  113. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  114. } else {
  115. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  116. }
  117. // XML header
  118. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  119. // cp:coreProperties
  120. $objWriter->startElement('cp:coreProperties');
  121. $objWriter->writeAttribute('xmlns:cp', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties');
  122. $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
  123. $objWriter->writeAttribute('xmlns:dcterms', 'http://purl.org/dc/terms/');
  124. $objWriter->writeAttribute('xmlns:dcmitype', 'http://purl.org/dc/dcmitype/');
  125. $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  126. // dc:creator
  127. $objWriter->writeElement('dc:creator', $pPHPExcel->getProperties()->getCreator());
  128. // cp:lastModifiedBy
  129. $objWriter->writeElement('cp:lastModifiedBy', $pPHPExcel->getProperties()->getLastModifiedBy());
  130. // dcterms:created
  131. $objWriter->startElement('dcterms:created');
  132. $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
  133. $objWriter->writeRawData(date(DATE_W3C, $pPHPExcel->getProperties()->getCreated()));
  134. $objWriter->endElement();
  135. // dcterms:modified
  136. $objWriter->startElement('dcterms:modified');
  137. $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
  138. $objWriter->writeRawData(date(DATE_W3C, $pPHPExcel->getProperties()->getModified()));
  139. $objWriter->endElement();
  140. // dc:title
  141. $objWriter->writeElement('dc:title', $pPHPExcel->getProperties()->getTitle());
  142. // dc:description
  143. $objWriter->writeElement('dc:description', $pPHPExcel->getProperties()->getDescription());
  144. // dc:subject
  145. $objWriter->writeElement('dc:subject', $pPHPExcel->getProperties()->getSubject());
  146. // cp:keywords
  147. $objWriter->writeElement('cp:keywords', $pPHPExcel->getProperties()->getKeywords());
  148. // cp:category
  149. $objWriter->writeElement('cp:category', $pPHPExcel->getProperties()->getCategory());
  150. $objWriter->endElement();
  151. // Return
  152. return $objWriter->getData();
  153. }
  154. /**
  155. * Write docProps/custom.xml to XML format
  156. *
  157. * @param PHPExcel $pPHPExcel
  158. * @return string XML Output
  159. * @throws PHPExcel_Writer_Exception
  160. */
  161. public function writeDocPropsCustom(PHPExcel $pPHPExcel = null)
  162. {
  163. $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
  164. if (empty($customPropertyList)) {
  165. return;
  166. }
  167. // Create XML writer
  168. $objWriter = null;
  169. if ($this->getParentWriter()->getUseDiskCaching()) {
  170. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  171. } else {
  172. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  173. }
  174. // XML header
  175. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  176. // cp:coreProperties
  177. $objWriter->startElement('Properties');
  178. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
  179. $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
  180. foreach ($customPropertyList as $key => $customProperty) {
  181. $propertyValue = $pPHPExcel->getProperties()->getCustomPropertyValue($customProperty);
  182. $propertyType = $pPHPExcel->getProperties()->getCustomPropertyType($customProperty);
  183. $objWriter->startElement('property');
  184. $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
  185. $objWriter->writeAttribute('pid', $key+2);
  186. $objWriter->writeAttribute('name', $customProperty);
  187. switch ($propertyType) {
  188. case 'i':
  189. $objWriter->writeElement('vt:i4', $propertyValue);
  190. break;
  191. case 'f':
  192. $objWriter->writeElement('vt:r8', $propertyValue);
  193. break;
  194. case 'b':
  195. $objWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false');
  196. break;
  197. case 'd':
  198. $objWriter->startElement('vt:filetime');
  199. $objWriter->writeRawData(date(DATE_W3C, $propertyValue));
  200. $objWriter->endElement();
  201. break;
  202. default:
  203. $objWriter->writeElement('vt:lpwstr', $propertyValue);
  204. break;
  205. }
  206. $objWriter->endElement();
  207. }
  208. $objWriter->endElement();
  209. return $objWriter->getData();
  210. }
  211. }