Comments.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Writer_Excel2007_Comments extends PHPExcel_Writer_Excel2007_WriterPart
  8. {
  9. /**
  10. * Write comments to XML format
  11. *
  12. * @param PHPExcel_Worksheet $pWorksheet
  13. * @return string XML Output
  14. * @throws PHPExcel_Writer_Exception
  15. */
  16. public function writeComments(PHPExcel_Worksheet $pWorksheet = 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. // Comments cache
  28. $comments = $pWorksheet->getComments();
  29. // Authors cache
  30. $authors = array();
  31. $authorId = 0;
  32. foreach ($comments as $comment) {
  33. if (!isset($authors[$comment->getAuthor()])) {
  34. $authors[$comment->getAuthor()] = $authorId++;
  35. }
  36. }
  37. // comments
  38. $objWriter->startElement('comments');
  39. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  40. // Loop through authors
  41. $objWriter->startElement('authors');
  42. foreach ($authors as $author => $index) {
  43. $objWriter->writeElement('author', $author);
  44. }
  45. $objWriter->endElement();
  46. // Loop through comments
  47. $objWriter->startElement('commentList');
  48. foreach ($comments as $key => $value) {
  49. $this->writeComment($objWriter, $key, $value, $authors);
  50. }
  51. $objWriter->endElement();
  52. $objWriter->endElement();
  53. // Return
  54. return $objWriter->getData();
  55. }
  56. /**
  57. * Write comment to XML format
  58. *
  59. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  60. * @param string $pCellReference Cell reference
  61. * @param PHPExcel_Comment $pComment Comment
  62. * @param array $pAuthors Array of authors
  63. * @throws PHPExcel_Writer_Exception
  64. */
  65. private function writeComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null, $pAuthors = null)
  66. {
  67. // comment
  68. $objWriter->startElement('comment');
  69. $objWriter->writeAttribute('ref', $pCellReference);
  70. $objWriter->writeAttribute('authorId', $pAuthors[$pComment->getAuthor()]);
  71. // text
  72. $objWriter->startElement('text');
  73. $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $pComment->getText());
  74. $objWriter->endElement();
  75. $objWriter->endElement();
  76. }
  77. /**
  78. * Write VML comments to XML format
  79. *
  80. * @param PHPExcel_Worksheet $pWorksheet
  81. * @return string XML Output
  82. * @throws PHPExcel_Writer_Exception
  83. */
  84. public function writeVMLComments(PHPExcel_Worksheet $pWorksheet = null)
  85. {
  86. // Create XML writer
  87. $objWriter = null;
  88. if ($this->getParentWriter()->getUseDiskCaching()) {
  89. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  90. } else {
  91. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  92. }
  93. // XML header
  94. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  95. // Comments cache
  96. $comments = $pWorksheet->getComments();
  97. // xml
  98. $objWriter->startElement('xml');
  99. $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
  100. $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
  101. $objWriter->writeAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
  102. // o:shapelayout
  103. $objWriter->startElement('o:shapelayout');
  104. $objWriter->writeAttribute('v:ext', 'edit');
  105. // o:idmap
  106. $objWriter->startElement('o:idmap');
  107. $objWriter->writeAttribute('v:ext', 'edit');
  108. $objWriter->writeAttribute('data', '1');
  109. $objWriter->endElement();
  110. $objWriter->endElement();
  111. // v:shapetype
  112. $objWriter->startElement('v:shapetype');
  113. $objWriter->writeAttribute('id', '_x0000_t202');
  114. $objWriter->writeAttribute('coordsize', '21600,21600');
  115. $objWriter->writeAttribute('o:spt', '202');
  116. $objWriter->writeAttribute('path', 'm,l,21600r21600,l21600,xe');
  117. // v:stroke
  118. $objWriter->startElement('v:stroke');
  119. $objWriter->writeAttribute('joinstyle', 'miter');
  120. $objWriter->endElement();
  121. // v:path
  122. $objWriter->startElement('v:path');
  123. $objWriter->writeAttribute('gradientshapeok', 't');
  124. $objWriter->writeAttribute('o:connecttype', 'rect');
  125. $objWriter->endElement();
  126. $objWriter->endElement();
  127. // Loop through comments
  128. foreach ($comments as $key => $value) {
  129. $this->writeVMLComment($objWriter, $key, $value);
  130. }
  131. $objWriter->endElement();
  132. // Return
  133. return $objWriter->getData();
  134. }
  135. /**
  136. * Write VML comment to XML format
  137. *
  138. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  139. * @param string $pCellReference Cell reference
  140. * @param PHPExcel_Comment $pComment Comment
  141. * @throws PHPExcel_Writer_Exception
  142. */
  143. private function writeVMLComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null)
  144. {
  145. // Metadata
  146. list($column, $row) = PHPExcel_Cell::coordinateFromString($pCellReference);
  147. $column = PHPExcel_Cell::columnIndexFromString($column);
  148. $id = 1024 + $column + $row;
  149. $id = substr($id, 0, 4);
  150. // v:shape
  151. $objWriter->startElement('v:shape');
  152. $objWriter->writeAttribute('id', '_x0000_s' . $id);
  153. $objWriter->writeAttribute('type', '#_x0000_t202');
  154. $objWriter->writeAttribute('style', 'position:absolute;margin-left:' . $pComment->getMarginLeft() . ';margin-top:' . $pComment->getMarginTop() . ';width:' . $pComment->getWidth() . ';height:' . $pComment->getHeight() . ';z-index:1;visibility:' . ($pComment->getVisible() ? 'visible' : 'hidden'));
  155. $objWriter->writeAttribute('fillcolor', '#' . $pComment->getFillColor()->getRGB());
  156. $objWriter->writeAttribute('o:insetmode', 'auto');
  157. // v:fill
  158. $objWriter->startElement('v:fill');
  159. $objWriter->writeAttribute('color2', '#' . $pComment->getFillColor()->getRGB());
  160. $objWriter->endElement();
  161. // v:shadow
  162. $objWriter->startElement('v:shadow');
  163. $objWriter->writeAttribute('on', 't');
  164. $objWriter->writeAttribute('color', 'black');
  165. $objWriter->writeAttribute('obscured', 't');
  166. $objWriter->endElement();
  167. // v:path
  168. $objWriter->startElement('v:path');
  169. $objWriter->writeAttribute('o:connecttype', 'none');
  170. $objWriter->endElement();
  171. // v:textbox
  172. $objWriter->startElement('v:textbox');
  173. $objWriter->writeAttribute('style', 'mso-direction-alt:auto');
  174. // div
  175. $objWriter->startElement('div');
  176. $objWriter->writeAttribute('style', 'text-align:left');
  177. $objWriter->endElement();
  178. $objWriter->endElement();
  179. // x:ClientData
  180. $objWriter->startElement('x:ClientData');
  181. $objWriter->writeAttribute('ObjectType', 'Note');
  182. // x:MoveWithCells
  183. $objWriter->writeElement('x:MoveWithCells', '');
  184. // x:SizeWithCells
  185. $objWriter->writeElement('x:SizeWithCells', '');
  186. // x:Anchor
  187. //$objWriter->writeElement('x:Anchor', $column . ', 15, ' . ($row - 2) . ', 10, ' . ($column + 4) . ', 15, ' . ($row + 5) . ', 18');
  188. // x:AutoFill
  189. $objWriter->writeElement('x:AutoFill', 'False');
  190. // x:Row
  191. $objWriter->writeElement('x:Row', ($row - 1));
  192. // x:Column
  193. $objWriter->writeElement('x:Column', ($column - 1));
  194. $objWriter->endElement();
  195. $objWriter->endElement();
  196. }
  197. }