AdvancedValueBinder.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. if (!defined('PHPEXCEL_ROOT')) {
  8. /**
  9. * @ignore
  10. */
  11. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  12. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  13. }
  14. /**
  15. * PHPExcel_Cell_AdvancedValueBinder
  16. *
  17. * Copyright (c) 2006 - 2015 PHPExcel
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2.1 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  32. *
  33. * @category PHPExcel
  34. * @package PHPExcel_Cell
  35. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  36. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  37. * @version ##VERSION##, ##DATE##
  38. */
  39. class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
  40. {
  41. /**
  42. * Bind value to a cell
  43. *
  44. * @param PHPExcel_Cell $cell Cell to bind value to
  45. * @param mixed $value Value to bind in cell
  46. * @return boolean
  47. */
  48. public function bindValue(PHPExcel_Cell $cell, $value = null)
  49. {
  50. // sanitize UTF-8 strings
  51. if (is_string($value)) {
  52. $value = PHPExcel_Shared_String::SanitizeUTF8($value);
  53. }
  54. // Find out data type
  55. $dataType = parent::dataTypeForValue($value);
  56. // Style logic - strings
  57. if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && !$value instanceof PHPExcel_RichText) {
  58. // Test for booleans using locale-setting
  59. if ($value == PHPExcel_Calculation::getTRUE()) {
  60. $cell->setValueExplicit(true, PHPExcel_Cell_DataType::TYPE_BOOL);
  61. return true;
  62. } elseif ($value == PHPExcel_Calculation::getFALSE()) {
  63. $cell->setValueExplicit(false, PHPExcel_Cell_DataType::TYPE_BOOL);
  64. return true;
  65. }
  66. // Check for number in scientific format
  67. if (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NUMBER.'$/', $value)) {
  68. $cell->setValueExplicit((float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  69. return true;
  70. }
  71. // Check for fraction
  72. if (preg_match('/^([+-]?)\s*([0-9]+)\s?\/\s*([0-9]+)$/', $value, $matches)) {
  73. // Convert value to number
  74. $value = $matches[2] / $matches[3];
  75. if ($matches[1] == '-') {
  76. $value = 0 - $value;
  77. }
  78. $cell->setValueExplicit((float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  79. // Set style
  80. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  81. ->getNumberFormat()->setFormatCode('??/??');
  82. return true;
  83. } elseif (preg_match('/^([+-]?)([0-9]*) +([0-9]*)\s?\/\s*([0-9]*)$/', $value, $matches)) {
  84. // Convert value to number
  85. $value = $matches[2] + ($matches[3] / $matches[4]);
  86. if ($matches[1] == '-') {
  87. $value = 0 - $value;
  88. }
  89. $cell->setValueExplicit((float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  90. // Set style
  91. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  92. ->getNumberFormat()->setFormatCode('# ??/??');
  93. return true;
  94. }
  95. // Check for percentage
  96. if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/', $value)) {
  97. // Convert value to number
  98. $value = (float) str_replace('%', '', $value) / 100;
  99. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  100. // Set style
  101. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  102. ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00);
  103. return true;
  104. }
  105. // Check for currency
  106. $currencyCode = PHPExcel_Shared_String::getCurrencyCode();
  107. $decimalSeparator = PHPExcel_Shared_String::getDecimalSeparator();
  108. $thousandsSeparator = PHPExcel_Shared_String::getThousandsSeparator();
  109. if (preg_match('/^'.preg_quote($currencyCode).' *(\d{1,3}('.preg_quote($thousandsSeparator).'\d{3})*|(\d+))('.preg_quote($decimalSeparator).'\d{2})?$/', $value)) {
  110. // Convert value to number
  111. $value = (float) trim(str_replace(array($currencyCode, $thousandsSeparator, $decimalSeparator), array('', '', '.'), $value));
  112. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  113. // Set style
  114. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  115. ->getNumberFormat()->setFormatCode(
  116. str_replace('$', $currencyCode, PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE)
  117. );
  118. return true;
  119. } elseif (preg_match('/^\$ *(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/', $value)) {
  120. // Convert value to number
  121. $value = (float) trim(str_replace(array('$',','), '', $value));
  122. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  123. // Set style
  124. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  125. ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  126. return true;
  127. }
  128. // Check for time without seconds e.g. '9:45', '09:45'
  129. if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/', $value)) {
  130. // Convert value to number
  131. list($h, $m) = explode(':', $value);
  132. $days = $h / 24 + $m / 1440;
  133. $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  134. // Set style
  135. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  136. ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3);
  137. return true;
  138. }
  139. // Check for time with seconds '9:45:59', '09:45:59'
  140. if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$/', $value)) {
  141. // Convert value to number
  142. list($h, $m, $s) = explode(':', $value);
  143. $days = $h / 24 + $m / 1440 + $s / 86400;
  144. // Convert value to number
  145. $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  146. // Set style
  147. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  148. ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
  149. return true;
  150. }
  151. // Check for datetime, e.g. '2008-12-31', '2008-12-31 15:59', '2008-12-31 15:59:10'
  152. if (($d = PHPExcel_Shared_Date::stringToExcel($value)) !== false) {
  153. // Convert value to number
  154. $cell->setValueExplicit($d, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  155. // Determine style. Either there is a time part or not. Look for ':'
  156. if (strpos($value, ':') !== false) {
  157. $formatCode = 'yyyy-mm-dd h:mm';
  158. } else {
  159. $formatCode = 'yyyy-mm-dd';
  160. }
  161. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  162. ->getNumberFormat()->setFormatCode($formatCode);
  163. return true;
  164. }
  165. // Check for newline character "\n"
  166. if (strpos($value, "\n") !== false) {
  167. $value = PHPExcel_Shared_String::SanitizeUTF8($value);
  168. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
  169. // Set style
  170. $cell->getWorksheet()->getStyle($cell->getCoordinate())
  171. ->getAlignment()->setWrapText(true);
  172. return true;
  173. }
  174. }
  175. // Not bound yet? Use parent...
  176. return parent::bindValue($cell, $value);
  177. }
  178. }