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

    作者:冯亮
             
    能力有限,水平一般。如有错误,欢迎指正
  • 相关阅读:
    意外发现,VC断点可加在构造函数的左括号上
    C++中的INL
    如何用DELPHI编程修改外部EXE文件的版本信
    j2ee面试宝典翻译(1)
    华为总裁任正非:允许小部分力量去颠覆性创新
    QStringList与QString互转
    QTreeView只显示指定驱动器及其目录,隐藏所有兄弟节点
    Protected Functions 是理解OO的难点和关键
    技术人员的创业陷阱:我能,但不管用户在哪里!
    大陆的创业环境和风气的确产生巨大变化,大众创业“蔚然成风”
  • 原文地址:https://www.cnblogs.com/fengliang/p/9996631.html
Copyright © 2011-2022 走看看