zoukankan      html  css  js  c++  java
  • maatwebsite lost precision when export long integer data

    Maatwebsite would lost precision when export long integer data, no matter string or int storaged in database.   Implementing MaatwebsiteExcelConcernsWithCustomValueBinder can solve this issue in your export class. You should change dataTypeForValue method like following.

     1 /** Bind value to a cell.
     2      *
     3      * @param Cell $cell Cell to bind value to
     4      * @param mixed $value Value to bind in cell
     5      *
     6      * @return bool
     7      */
     8     public function bindValue(Cell $cell, $value)
     9     {
    10         // sanitize UTF-8 strings
    11         if (is_string($value)) {
    12             $value = StringHelper::sanitizeUTF8($value);
    13         } elseif (is_object($value)) {
    14             // Handle any objects that might be injected
    15             if ($value instanceof DateTimeInterface) {
    16                 $value = $value->format('Y-m-d H:i:s');
    17             } elseif (!($value instanceof RichText)) {
    18                 $value = (string) $value;
    19             }
    20         }
    21 
    22         // Set value explicit
    23         $cell->setValueExplicit($value, static::dataTypeForValue($value));
    24 
    25         // Done!
    26         return true;
    27     }
    28 
    29     /**
    30      * DataType for value.
    31      *
    32      * @param mixed $pValue
    33      *
    34      * @return string
    35      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
    36      */
    37     public static function dataTypeForValue($pValue)
    38     {
    39         // Match the value against a few data types
    40         if ($pValue === null) {
    41             return DataType::TYPE_NULL;
    42         } elseif ($pValue === '') {
    43             return DataType::TYPE_STRING;
    44         } elseif ($pValue instanceof RichText) {
    45             return DataType::TYPE_INLINE;
    46         } elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
    47             return DataType::TYPE_FORMULA;
    48         } elseif (is_bool($pValue)) {
    49             return DataType::TYPE_BOOL;
    50         } elseif (is_float($pValue) || is_int($pValue)) {
    51             return DataType::TYPE_NUMERIC;
    52         } elseif (preg_match('/^[+-]?(d+\.?d*|d*\.?d+)([Ee][-+]?[0-2]?d{1,3})?$/', $pValue)) {
    53             $tValue = ltrim($pValue, '+-');
    54             if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
    55                 return DataType::TYPE_STRING;
    56             } elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
    57                 return DataType::TYPE_STRING;
    58             }
    59 
    60             return DataType::TYPE_STRING;
    61         } elseif (is_string($pValue)) {
    62             $errorCodes = DataType::getErrorCodes();
    63             if (isset($errorCodes[$pValue])) {
    64                 return DataType::TYPE_ERROR;
    65             }
    66         }
    67 
    68         return DataType::TYPE_STRING;
    69     }

    The only one different is in line 60, author return  DataType::TYPE_NUMERIC

    作者:冯亮
             
    能力有限,水平一般。如有错误,欢迎指正
  • 相关阅读:
    rsyslog 存储到 mysql
    LAMP 建立 Wordpress 站点 Linux Apache MariaDB PHP
    CentOS 6.9 CentOS 7.4 自动安装系统 kickstart
    shell编程, 100文钱买100只鸡, 简单实现
    创建私有CA, 加密解密基础, PKI, SSL
    运维派 企业面试题6 防dos攻击
    运维派 企业面试题4&5 创建10个 用户 ; ping探测主机是否在线
    运维派 企业面试题3 为上题中的 "十个随机字母_test.html" 文件 更名
    运维派 企业面试题2 创建10个 "十个随机字母_test.html" 文件
    MongoDB释放磁盘空间
  • 原文地址:https://www.cnblogs.com/fengliang/p/9996631.html
Copyright © 2011-2022 走看看