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

    作者:冯亮
             
    能力有限,水平一般。如有错误,欢迎指正
  • 相关阅读:
    Emgucv使用中常用函数总结
    设置 omitempty 标签忽略空值字段
    Goland 中定义实时模板
    gorm Update
    Gorm 日期格式错误
    Gorm 多张表的自动迁移
    Sql取出各科分数前三名的学生,Sql各科成绩前三的学生
    美团Leaf分布式ID Leaf安装和使用,美团Leaf snowflake雪花算法模式,美团Leaf segment号段模式
    RocketMQ可靠消息最终一致性解决方案
    Windows安装RocketMQ,RocketMQ Windows安装和使用
  • 原文地址:https://www.cnblogs.com/fengliang/p/9996631.html
Copyright © 2011-2022 走看看