AdvancedValueBinder.php 8.7 KB

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