OpenDocument.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Writer_OpenDocument extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  8. {
  9. /**
  10. * Private writer parts
  11. *
  12. * @var PHPExcel_Writer_OpenDocument_WriterPart[]
  13. */
  14. private $writerParts = array();
  15. /**
  16. * Private PHPExcel
  17. *
  18. * @var PHPExcel
  19. */
  20. private $spreadSheet;
  21. /**
  22. * Create a new PHPExcel_Writer_OpenDocument
  23. *
  24. * @param PHPExcel $pPHPExcel
  25. */
  26. public function __construct(PHPExcel $pPHPExcel = null)
  27. {
  28. $this->setPHPExcel($pPHPExcel);
  29. $writerPartsArray = array(
  30. 'content' => 'PHPExcel_Writer_OpenDocument_Content',
  31. 'meta' => 'PHPExcel_Writer_OpenDocument_Meta',
  32. 'meta_inf' => 'PHPExcel_Writer_OpenDocument_MetaInf',
  33. 'mimetype' => 'PHPExcel_Writer_OpenDocument_Mimetype',
  34. 'settings' => 'PHPExcel_Writer_OpenDocument_Settings',
  35. 'styles' => 'PHPExcel_Writer_OpenDocument_Styles',
  36. 'thumbnails' => 'PHPExcel_Writer_OpenDocument_Thumbnails'
  37. );
  38. foreach ($writerPartsArray as $writer => $class) {
  39. $this->writerParts[$writer] = new $class($this);
  40. }
  41. }
  42. /**
  43. * Get writer part
  44. *
  45. * @param string $pPartName Writer part name
  46. * @return PHPExcel_Writer_Excel2007_WriterPart
  47. */
  48. public function getWriterPart($pPartName = '')
  49. {
  50. if ($pPartName != '' && isset($this->writerParts[strtolower($pPartName)])) {
  51. return $this->writerParts[strtolower($pPartName)];
  52. } else {
  53. return null;
  54. }
  55. }
  56. /**
  57. * Save PHPExcel to file
  58. *
  59. * @param string $pFilename
  60. * @throws PHPExcel_Writer_Exception
  61. */
  62. public function save($pFilename = null)
  63. {
  64. if (!$this->spreadSheet) {
  65. throw new PHPExcel_Writer_Exception('PHPExcel object unassigned.');
  66. }
  67. // garbage collect
  68. $this->spreadSheet->garbageCollect();
  69. // If $pFilename is php://output or php://stdout, make it a temporary file...
  70. $originalFilename = $pFilename;
  71. if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
  72. $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
  73. if ($pFilename == '') {
  74. $pFilename = $originalFilename;
  75. }
  76. }
  77. $objZip = $this->createZip($pFilename);
  78. $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->writeManifest());
  79. $objZip->addFromString('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->writeThumbnail());
  80. $objZip->addFromString('content.xml', $this->getWriterPart('content')->write());
  81. $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->write());
  82. $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->write());
  83. $objZip->addFromString('settings.xml', $this->getWriterPart('settings')->write());
  84. $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->write());
  85. // Close file
  86. if ($objZip->close() === false) {
  87. throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
  88. }
  89. // If a temporary file was used, copy it to the correct file stream
  90. if ($originalFilename != $pFilename) {
  91. if (copy($pFilename, $originalFilename) === false) {
  92. throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  93. }
  94. @unlink($pFilename);
  95. }
  96. }
  97. /**
  98. * Create zip object
  99. *
  100. * @param string $pFilename
  101. * @throws PHPExcel_Writer_Exception
  102. * @return ZipArchive
  103. */
  104. private function createZip($pFilename)
  105. {
  106. // Create new ZIP file and open it for writing
  107. $zipClass = PHPExcel_Settings::getZipClass();
  108. $objZip = new $zipClass();
  109. // Retrieve OVERWRITE and CREATE constants from the instantiated zip class
  110. // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
  111. $ro = new ReflectionObject($objZip);
  112. $zipOverWrite = $ro->getConstant('OVERWRITE');
  113. $zipCreate = $ro->getConstant('CREATE');
  114. if (file_exists($pFilename)) {
  115. unlink($pFilename);
  116. }
  117. // Try opening the ZIP file
  118. if ($objZip->open($pFilename, $zipOverWrite) !== true) {
  119. if ($objZip->open($pFilename, $zipCreate) !== true) {
  120. throw new PHPExcel_Writer_Exception("Could not open $pFilename for writing.");
  121. }
  122. }
  123. return $objZip;
  124. }
  125. /**
  126. * Get PHPExcel object
  127. *
  128. * @return PHPExcel
  129. * @throws PHPExcel_Writer_Exception
  130. */
  131. public function getPHPExcel()
  132. {
  133. if ($this->spreadSheet !== null) {
  134. return $this->spreadSheet;
  135. } else {
  136. throw new PHPExcel_Writer_Exception('No PHPExcel assigned.');
  137. }
  138. }
  139. /**
  140. * Set PHPExcel object
  141. *
  142. * @param PHPExcel $pPHPExcel PHPExcel object
  143. * @throws PHPExcel_Writer_Exception
  144. * @return PHPExcel_Writer_Excel2007
  145. */
  146. public function setPHPExcel(PHPExcel $pPHPExcel = null)
  147. {
  148. $this->spreadSheet = $pPHPExcel;
  149. return $this;
  150. }
  151. }