Rels.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
  8. {
  9. /**
  10. * Write relationships to XML format
  11. *
  12. * @param PHPExcel $pPHPExcel
  13. * @return string XML Output
  14. * @throws PHPExcel_Writer_Exception
  15. */
  16. public function writeRelationships(PHPExcel $pPHPExcel = null)
  17. {
  18. // Create XML writer
  19. $objWriter = null;
  20. if ($this->getParentWriter()->getUseDiskCaching()) {
  21. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  22. } else {
  23. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  24. }
  25. // XML header
  26. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  27. // Relationships
  28. $objWriter->startElement('Relationships');
  29. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  30. $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
  31. if (!empty($customPropertyList)) {
  32. // Relationship docProps/app.xml
  33. $this->writeRelationship(
  34. $objWriter,
  35. 4,
  36. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
  37. 'docProps/custom.xml'
  38. );
  39. }
  40. // Relationship docProps/app.xml
  41. $this->writeRelationship(
  42. $objWriter,
  43. 3,
  44. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  45. 'docProps/app.xml'
  46. );
  47. // Relationship docProps/core.xml
  48. $this->writeRelationship(
  49. $objWriter,
  50. 2,
  51. 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  52. 'docProps/core.xml'
  53. );
  54. // Relationship xl/workbook.xml
  55. $this->writeRelationship(
  56. $objWriter,
  57. 1,
  58. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  59. 'xl/workbook.xml'
  60. );
  61. // a custom UI in workbook ?
  62. if ($pPHPExcel->hasRibbon()) {
  63. $this->writeRelationShip(
  64. $objWriter,
  65. 5,
  66. 'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility',
  67. $pPHPExcel->getRibbonXMLData('target')
  68. );
  69. }
  70. $objWriter->endElement();
  71. return $objWriter->getData();
  72. }
  73. /**
  74. * Write workbook relationships to XML format
  75. *
  76. * @param PHPExcel $pPHPExcel
  77. * @return string XML Output
  78. * @throws PHPExcel_Writer_Exception
  79. */
  80. public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
  81. {
  82. // Create XML writer
  83. $objWriter = null;
  84. if ($this->getParentWriter()->getUseDiskCaching()) {
  85. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  86. } else {
  87. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  88. }
  89. // XML header
  90. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  91. // Relationships
  92. $objWriter->startElement('Relationships');
  93. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  94. // Relationship styles.xml
  95. $this->writeRelationship(
  96. $objWriter,
  97. 1,
  98. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
  99. 'styles.xml'
  100. );
  101. // Relationship theme/theme1.xml
  102. $this->writeRelationship(
  103. $objWriter,
  104. 2,
  105. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
  106. 'theme/theme1.xml'
  107. );
  108. // Relationship sharedStrings.xml
  109. $this->writeRelationship(
  110. $objWriter,
  111. 3,
  112. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
  113. 'sharedStrings.xml'
  114. );
  115. // Relationships with sheets
  116. $sheetCount = $pPHPExcel->getSheetCount();
  117. for ($i = 0; $i < $sheetCount; ++$i) {
  118. $this->writeRelationship(
  119. $objWriter,
  120. ($i + 1 + 3),
  121. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
  122. 'worksheets/sheet' . ($i + 1) . '.xml'
  123. );
  124. }
  125. // Relationships for vbaProject if needed
  126. // id : just after the last sheet
  127. if ($pPHPExcel->hasMacros()) {
  128. $this->writeRelationShip(
  129. $objWriter,
  130. ($i + 1 + 3),
  131. 'http://schemas.microsoft.com/office/2006/relationships/vbaProject',
  132. 'vbaProject.bin'
  133. );
  134. ++$i;//increment i if needed for an another relation
  135. }
  136. $objWriter->endElement();
  137. return $objWriter->getData();
  138. }
  139. /**
  140. * Write worksheet relationships to XML format
  141. *
  142. * Numbering is as follows:
  143. * rId1 - Drawings
  144. * rId_hyperlink_x - Hyperlinks
  145. *
  146. * @param PHPExcel_Worksheet $pWorksheet
  147. * @param int $pWorksheetId
  148. * @param boolean $includeCharts Flag indicating if we should write charts
  149. * @return string XML Output
  150. * @throws PHPExcel_Writer_Exception
  151. */
  152. public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1, $includeCharts = false)
  153. {
  154. // Create XML writer
  155. $objWriter = null;
  156. if ($this->getParentWriter()->getUseDiskCaching()) {
  157. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  158. } else {
  159. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  160. }
  161. // XML header
  162. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  163. // Relationships
  164. $objWriter->startElement('Relationships');
  165. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  166. // Write drawing relationships?
  167. $d = 0;
  168. if ($includeCharts) {
  169. $charts = $pWorksheet->getChartCollection();
  170. } else {
  171. $charts = array();
  172. }
  173. if (($pWorksheet->getDrawingCollection()->count() > 0) ||
  174. (count($charts) > 0)) {
  175. $this->writeRelationship(
  176. $objWriter,
  177. ++$d,
  178. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  179. '../drawings/drawing' . $pWorksheetId . '.xml'
  180. );
  181. }
  182. // Write chart relationships?
  183. // $chartCount = 0;
  184. // $charts = $pWorksheet->getChartCollection();
  185. // echo 'Chart Rels: ' , count($charts) , '<br />';
  186. // if (count($charts) > 0) {
  187. // foreach ($charts as $chart) {
  188. // $this->writeRelationship(
  189. // $objWriter,
  190. // ++$d,
  191. // 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
  192. // '../charts/chart' . ++$chartCount . '.xml'
  193. // );
  194. // }
  195. // }
  196. //
  197. // Write hyperlink relationships?
  198. $i = 1;
  199. foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
  200. if (!$hyperlink->isInternal()) {
  201. $this->writeRelationship(
  202. $objWriter,
  203. '_hyperlink_' . $i,
  204. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
  205. $hyperlink->getUrl(),
  206. 'External'
  207. );
  208. ++$i;
  209. }
  210. }
  211. // Write comments relationship?
  212. $i = 1;
  213. if (count($pWorksheet->getComments()) > 0) {
  214. $this->writeRelationship(
  215. $objWriter,
  216. '_comments_vml' . $i,
  217. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  218. '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
  219. );
  220. $this->writeRelationship(
  221. $objWriter,
  222. '_comments' . $i,
  223. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
  224. '../comments' . $pWorksheetId . '.xml'
  225. );
  226. }
  227. // Write header/footer relationship?
  228. $i = 1;
  229. if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
  230. $this->writeRelationship(
  231. $objWriter,
  232. '_headerfooter_vml' . $i,
  233. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  234. '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
  235. );
  236. }
  237. $objWriter->endElement();
  238. return $objWriter->getData();
  239. }
  240. /**
  241. * Write drawing relationships to XML format
  242. *
  243. * @param PHPExcel_Worksheet $pWorksheet
  244. * @param int &$chartRef Chart ID
  245. * @param boolean $includeCharts Flag indicating if we should write charts
  246. * @return string XML Output
  247. * @throws PHPExcel_Writer_Exception
  248. */
  249. public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $includeCharts = false)
  250. {
  251. // Create XML writer
  252. $objWriter = null;
  253. if ($this->getParentWriter()->getUseDiskCaching()) {
  254. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  255. } else {
  256. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  257. }
  258. // XML header
  259. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  260. // Relationships
  261. $objWriter->startElement('Relationships');
  262. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  263. // Loop through images and write relationships
  264. $i = 1;
  265. $iterator = $pWorksheet->getDrawingCollection()->getIterator();
  266. while ($iterator->valid()) {
  267. if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
  268. || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
  269. // Write relationship for image drawing
  270. $this->writeRelationship(
  271. $objWriter,
  272. $i,
  273. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  274. '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
  275. );
  276. }
  277. $iterator->next();
  278. ++$i;
  279. }
  280. if ($includeCharts) {
  281. // Loop through charts and write relationships
  282. $chartCount = $pWorksheet->getChartCount();
  283. if ($chartCount > 0) {
  284. for ($c = 0; $c < $chartCount; ++$c) {
  285. $this->writeRelationship(
  286. $objWriter,
  287. $i++,
  288. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
  289. '../charts/chart' . ++$chartRef . '.xml'
  290. );
  291. }
  292. }
  293. }
  294. $objWriter->endElement();
  295. return $objWriter->getData();
  296. }
  297. /**
  298. * Write header/footer drawing relationships to XML format
  299. *
  300. * @param PHPExcel_Worksheet $pWorksheet
  301. * @return string XML Output
  302. * @throws PHPExcel_Writer_Exception
  303. */
  304. public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
  305. {
  306. // Create XML writer
  307. $objWriter = null;
  308. if ($this->getParentWriter()->getUseDiskCaching()) {
  309. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  310. } else {
  311. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  312. }
  313. // XML header
  314. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  315. // Relationships
  316. $objWriter->startElement('Relationships');
  317. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  318. // Loop through images and write relationships
  319. foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
  320. // Write relationship for image drawing
  321. $this->writeRelationship(
  322. $objWriter,
  323. $key,
  324. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  325. '../media/' . $value->getIndexedFilename()
  326. );
  327. }
  328. $objWriter->endElement();
  329. return $objWriter->getData();
  330. }
  331. /**
  332. * Write Override content type
  333. *
  334. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  335. * @param int $pId Relationship ID. rId will be prepended!
  336. * @param string $pType Relationship type
  337. * @param string $pTarget Relationship target
  338. * @param string $pTargetMode Relationship target mode
  339. * @throws PHPExcel_Writer_Exception
  340. */
  341. private function writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
  342. {
  343. if ($pType != '' && $pTarget != '') {
  344. // Write relationship
  345. $objWriter->startElement('Relationship');
  346. $objWriter->writeAttribute('Id', 'rId' . $pId);
  347. $objWriter->writeAttribute('Type', $pType);
  348. $objWriter->writeAttribute('Target', $pTarget);
  349. if ($pTargetMode != '') {
  350. $objWriter->writeAttribute('TargetMode', $pTargetMode);
  351. }
  352. $objWriter->endElement();
  353. } else {
  354. throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
  355. }
  356. }
  357. }