Excel5.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Shared_Excel5
  8. {
  9. /**
  10. * Get the width of a column in pixels. We use the relationship y = ceil(7x) where
  11. * x is the width in intrinsic Excel units (measuring width in number of normal characters)
  12. * This holds for Arial 10
  13. *
  14. * @param PHPExcel_Worksheet $sheet The sheet
  15. * @param string $col The column
  16. * @return integer The width in pixels
  17. */
  18. public static function sizeCol($sheet, $col = 'A')
  19. {
  20. // default font of the workbook
  21. $font = $sheet->getParent()->getDefaultStyle()->getFont();
  22. $columnDimensions = $sheet->getColumnDimensions();
  23. // first find the true column width in pixels (uncollapsed and unhidden)
  24. if (isset($columnDimensions[$col]) and $columnDimensions[$col]->getWidth() != -1) {
  25. // then we have column dimension with explicit width
  26. $columnDimension = $columnDimensions[$col];
  27. $width = $columnDimension->getWidth();
  28. $pixelWidth = PHPExcel_Shared_Drawing::cellDimensionToPixels($width, $font);
  29. } elseif ($sheet->getDefaultColumnDimension()->getWidth() != -1) {
  30. // then we have default column dimension with explicit width
  31. $defaultColumnDimension = $sheet->getDefaultColumnDimension();
  32. $width = $defaultColumnDimension->getWidth();
  33. $pixelWidth = PHPExcel_Shared_Drawing::cellDimensionToPixels($width, $font);
  34. } else {
  35. // we don't even have any default column dimension. Width depends on default font
  36. $pixelWidth = PHPExcel_Shared_Font::getDefaultColumnWidthByFont($font, true);
  37. }
  38. // now find the effective column width in pixels
  39. if (isset($columnDimensions[$col]) and !$columnDimensions[$col]->getVisible()) {
  40. $effectivePixelWidth = 0;
  41. } else {
  42. $effectivePixelWidth = $pixelWidth;
  43. }
  44. return $effectivePixelWidth;
  45. }
  46. /**
  47. * Convert the height of a cell from user's units to pixels. By interpolation
  48. * the relationship is: y = 4/3x. If the height hasn't been set by the user we
  49. * use the default value. If the row is hidden we use a value of zero.
  50. *
  51. * @param PHPExcel_Worksheet $sheet The sheet
  52. * @param integer $row The row index (1-based)
  53. * @return integer The width in pixels
  54. */
  55. public static function sizeRow($sheet, $row = 1)
  56. {
  57. // default font of the workbook
  58. $font = $sheet->getParent()->getDefaultStyle()->getFont();
  59. $rowDimensions = $sheet->getRowDimensions();
  60. // first find the true row height in pixels (uncollapsed and unhidden)
  61. if (isset($rowDimensions[$row]) and $rowDimensions[$row]->getRowHeight() != -1) {
  62. // then we have a row dimension
  63. $rowDimension = $rowDimensions[$row];
  64. $rowHeight = $rowDimension->getRowHeight();
  65. $pixelRowHeight = (int) ceil(4 * $rowHeight / 3); // here we assume Arial 10
  66. } elseif ($sheet->getDefaultRowDimension()->getRowHeight() != -1) {
  67. // then we have a default row dimension with explicit height
  68. $defaultRowDimension = $sheet->getDefaultRowDimension();
  69. $rowHeight = $defaultRowDimension->getRowHeight();
  70. $pixelRowHeight = PHPExcel_Shared_Drawing::pointsToPixels($rowHeight);
  71. } else {
  72. // we don't even have any default row dimension. Height depends on default font
  73. $pointRowHeight = PHPExcel_Shared_Font::getDefaultRowHeightByFont($font);
  74. $pixelRowHeight = PHPExcel_Shared_Font::fontSizeToPixels($pointRowHeight);
  75. }
  76. // now find the effective row height in pixels
  77. if (isset($rowDimensions[$row]) and !$rowDimensions[$row]->getVisible()) {
  78. $effectivePixelRowHeight = 0;
  79. } else {
  80. $effectivePixelRowHeight = $pixelRowHeight;
  81. }
  82. return $effectivePixelRowHeight;
  83. }
  84. /**
  85. * Get the horizontal distance in pixels between two anchors
  86. * The distanceX is found as sum of all the spanning columns widths minus correction for the two offsets
  87. *
  88. * @param PHPExcel_Worksheet $sheet
  89. * @param string $startColumn
  90. * @param integer $startOffsetX Offset within start cell measured in 1/1024 of the cell width
  91. * @param string $endColumn
  92. * @param integer $endOffsetX Offset within end cell measured in 1/1024 of the cell width
  93. * @return integer Horizontal measured in pixels
  94. */
  95. public static function getDistanceX(PHPExcel_Worksheet $sheet, $startColumn = 'A', $startOffsetX = 0, $endColumn = 'A', $endOffsetX = 0)
  96. {
  97. $distanceX = 0;
  98. // add the widths of the spanning columns
  99. $startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1; // 1-based
  100. $endColumnIndex = PHPExcel_Cell::columnIndexFromString($endColumn) - 1; // 1-based
  101. for ($i = $startColumnIndex; $i <= $endColumnIndex; ++$i) {
  102. $distanceX += self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($i));
  103. }
  104. // correct for offsetX in startcell
  105. $distanceX -= (int) floor(self::sizeCol($sheet, $startColumn) * $startOffsetX / 1024);
  106. // correct for offsetX in endcell
  107. $distanceX -= (int) floor(self::sizeCol($sheet, $endColumn) * (1 - $endOffsetX / 1024));
  108. return $distanceX;
  109. }
  110. /**
  111. * Get the vertical distance in pixels between two anchors
  112. * The distanceY is found as sum of all the spanning rows minus two offsets
  113. *
  114. * @param PHPExcel_Worksheet $sheet
  115. * @param integer $startRow (1-based)
  116. * @param integer $startOffsetY Offset within start cell measured in 1/256 of the cell height
  117. * @param integer $endRow (1-based)
  118. * @param integer $endOffsetY Offset within end cell measured in 1/256 of the cell height
  119. * @return integer Vertical distance measured in pixels
  120. */
  121. public static function getDistanceY(PHPExcel_Worksheet $sheet, $startRow = 1, $startOffsetY = 0, $endRow = 1, $endOffsetY = 0)
  122. {
  123. $distanceY = 0;
  124. // add the widths of the spanning rows
  125. for ($row = $startRow; $row <= $endRow; ++$row) {
  126. $distanceY += self::sizeRow($sheet, $row);
  127. }
  128. // correct for offsetX in startcell
  129. $distanceY -= (int) floor(self::sizeRow($sheet, $startRow) * $startOffsetY / 256);
  130. // correct for offsetX in endcell
  131. $distanceY -= (int) floor(self::sizeRow($sheet, $endRow) * (1 - $endOffsetY / 256));
  132. return $distanceY;
  133. }
  134. /**
  135. * Convert 1-cell anchor coordinates to 2-cell anchor coordinates
  136. * This function is ported from PEAR Spreadsheet_Writer_Excel with small modifications
  137. *
  138. * Calculate the vertices that define the position of the image as required by
  139. * the OBJ record.
  140. *
  141. * +------------+------------+
  142. * | A | B |
  143. * +-----+------------+------------+
  144. * | |(x1,y1) | |
  145. * | 1 |(A1)._______|______ |
  146. * | | | | |
  147. * | | | | |
  148. * +-----+----| BITMAP |-----+
  149. * | | | | |
  150. * | 2 | |______________. |
  151. * | | | (B2)|
  152. * | | | (x2,y2)|
  153. * +---- +------------+------------+
  154. *
  155. * Example of a bitmap that covers some of the area from cell A1 to cell B2.
  156. *
  157. * Based on the width and height of the bitmap we need to calculate 8 vars:
  158. * $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
  159. * The width and height of the cells are also variable and have to be taken into
  160. * account.
  161. * The values of $col_start and $row_start are passed in from the calling
  162. * function. The values of $col_end and $row_end are calculated by subtracting
  163. * the width and height of the bitmap from the width and height of the
  164. * underlying cells.
  165. * The vertices are expressed as a percentage of the underlying cell width as
  166. * follows (rhs values are in pixels):
  167. *
  168. * x1 = X / W *1024
  169. * y1 = Y / H *256
  170. * x2 = (X-1) / W *1024
  171. * y2 = (Y-1) / H *256
  172. *
  173. * Where: X is distance from the left side of the underlying cell
  174. * Y is distance from the top of the underlying cell
  175. * W is the width of the cell
  176. * H is the height of the cell
  177. *
  178. * @param PHPExcel_Worksheet $sheet
  179. * @param string $coordinates E.g. 'A1'
  180. * @param integer $offsetX Horizontal offset in pixels
  181. * @param integer $offsetY Vertical offset in pixels
  182. * @param integer $width Width in pixels
  183. * @param integer $height Height in pixels
  184. * @return array
  185. */
  186. public static function oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height)
  187. {
  188. list($column, $row) = PHPExcel_Cell::coordinateFromString($coordinates);
  189. $col_start = PHPExcel_Cell::columnIndexFromString($column) - 1;
  190. $row_start = $row - 1;
  191. $x1 = $offsetX;
  192. $y1 = $offsetY;
  193. // Initialise end cell to the same as the start cell
  194. $col_end = $col_start; // Col containing lower right corner of object
  195. $row_end = $row_start; // Row containing bottom right corner of object
  196. // Zero the specified offset if greater than the cell dimensions
  197. if ($x1 >= self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_start))) {
  198. $x1 = 0;
  199. }
  200. if ($y1 >= self::sizeRow($sheet, $row_start + 1)) {
  201. $y1 = 0;
  202. }
  203. $width = $width + $x1 -1;
  204. $height = $height + $y1 -1;
  205. // Subtract the underlying cell widths to find the end cell of the image
  206. while ($width >= self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end))) {
  207. $width -= self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end));
  208. ++$col_end;
  209. }
  210. // Subtract the underlying cell heights to find the end cell of the image
  211. while ($height >= self::sizeRow($sheet, $row_end + 1)) {
  212. $height -= self::sizeRow($sheet, $row_end + 1);
  213. ++$row_end;
  214. }
  215. // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
  216. // with zero height or width.
  217. if (self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_start)) == 0) {
  218. return;
  219. }
  220. if (self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end)) == 0) {
  221. return;
  222. }
  223. if (self::sizeRow($sheet, $row_start + 1) == 0) {
  224. return;
  225. }
  226. if (self::sizeRow($sheet, $row_end + 1) == 0) {
  227. return;
  228. }
  229. // Convert the pixel values to the percentage value expected by Excel
  230. $x1 = $x1 / self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_start)) * 1024;
  231. $y1 = $y1 / self::sizeRow($sheet, $row_start + 1) * 256;
  232. $x2 = ($width + 1) / self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end)) * 1024; // Distance to right side of object
  233. $y2 = ($height + 1) / self::sizeRow($sheet, $row_end + 1) * 256; // Distance to bottom of object
  234. $startCoordinates = PHPExcel_Cell::stringFromColumnIndex($col_start) . ($row_start + 1);
  235. $endCoordinates = PHPExcel_Cell::stringFromColumnIndex($col_end) . ($row_end + 1);
  236. $twoAnchor = array(
  237. 'startCoordinates' => $startCoordinates,
  238. 'startOffsetX' => $x1,
  239. 'startOffsetY' => $y1,
  240. 'endCoordinates' => $endCoordinates,
  241. 'endOffsetX' => $x2,
  242. 'endOffsetY' => $y2,
  243. );
  244. return $twoAnchor;
  245. }
  246. }