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 \Maatwebsite\Excel\Concerns\WithCustomValueBinder 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

posted on 2018-11-21 17:53  冯亮  阅读(247)  评论(0编辑  收藏  举报

导航