19

I have an export to excel button which downloads excel file. However when i click it shows me error as Trying to access array offset on value of type int

My code is this:

public static function dataTypeForValue($pValue = null)
    {
        // Match the value against a few data types
        if ($pValue === null) {
            return PHPExcel_Cell_DataType::TYPE_NULL;
        } elseif ($pValue === '') {
            return PHPExcel_Cell_DataType::TYPE_STRING;
        } elseif ($pValue instanceof PHPExcel_RichText) {
            return PHPExcel_Cell_DataType::TYPE_INLINE;
  } elseif ($pValue[0] === '=' && strlen($pValue) > 1) {   //error comes in this line
            return PHPExcel_Cell_DataType::TYPE_FORMULA;
        } elseif (is_bool($pValue)) {
            return PHPExcel_Cell_DataType::TYPE_BOOL;
        } elseif (is_float($pValue) || is_int($pValue)) {
            return PHPExcel_Cell_DataType::TYPE_NUMERIC;
        } elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
            $tValue = ltrim($pValue, '+-');
            if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
                return PHPExcel_Cell_DataType::TYPE_STRING;
            } elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
                return PHPExcel_Cell_DataType::TYPE_STRING;
            }
            return PHPExcel_Cell_DataType::TYPE_NUMERIC;
        } elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) {
            return PHPExcel_Cell_DataType::TYPE_ERROR;
        }

        return PHPExcel_Cell_DataType::TYPE_STRING;
    }
}

what can be the solution for this..?

1
  • 1
    Which line here is line 82? Commented Jun 24, 2020 at 17:20

3 Answers 3

66

In the PHPExcel file "DefaultValueBinder.php", replace this line 82:

} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {

with the following:

} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
Sign up to request clarification or add additional context in comments.

1 Comment

This "fix" makes no more sense than the original code. It should be is_string($pValue) && $pValue[0] === '=' && strlen($pValue) > 1, the same way as its done couple lines below. And also you must change $tValue{0} to $tValue[0] there. Or ditch that PHPExcel that was discontinued many years ago, and use PHPSpreadsheet instead.
5

(is_string($pValue) && $pValue[0] === '=' && strlen($pValue) > 1) should work. $pValue[0] will not work with integers so check if it is an int or not before continuing.

2 Comments

$pValue is a string
Well based on information you gave and on error code it looks like $pValue is an int. Where are you getting this value from?
-1

This is work for me,
first, create custom class to overwrite valueBinder, copy function bindValue and dataTypeForValue on file DefaultValueBinder.php, and then replace

} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {

with the following:

} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {

so the file will be like this

<?php
namespace App\Excel;

use PHPExcel_Cell;
use PHPExcel_Cell_DataType;
use PHPExcel_Cell_DefaultValueBinder;
use PHPExcel_Cell_IValueBinder;
use PHPExcel_RichText;
use PHPExcel_Shared_String;

class CustomValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // sanitize UTF-8 strings
        if (is_string($value)) {
            $value = PHPExcel_Shared_String::SanitizeUTF8($value);
        } elseif (is_object($value)) {
            // Handle any objects that might be injected
            if ($value instanceof DateTime) {
                $value = $value->format('Y-m-d H:i:s');
            } elseif (!($value instanceof PHPExcel_RichText)) {
                $value = (string) $value;
            }
        }

        // Set value explicit
        $cell->setValueExplicit($value, self::dataTypeForValue($value));

        // Done!
        return true;
    }

    public static function dataTypeForValue($pValue = null)
    {
        // Match the value against a few data types
        if ($pValue === null) {
            return PHPExcel_Cell_DataType::TYPE_NULL;
        } elseif ($pValue === '') {
            return PHPExcel_Cell_DataType::TYPE_STRING;
        } elseif ($pValue instanceof PHPExcel_RichText) {
            return PHPExcel_Cell_DataType::TYPE_INLINE;
        } elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
            return PHPExcel_Cell_DataType::TYPE_FORMULA;
        } elseif (is_bool($pValue)) {
            return PHPExcel_Cell_DataType::TYPE_BOOL;
        } elseif (is_float($pValue) || is_int($pValue)) {
            return PHPExcel_Cell_DataType::TYPE_NUMERIC;
        } elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
            $tValue = ltrim($pValue, '+-');
            if (is_string($pValue) && $tValue{0} === '0' && strlen($tValue) > 1 && $tValue{1} !== '.') {
                return PHPExcel_Cell_DataType::TYPE_STRING;
            } elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
                return PHPExcel_Cell_DataType::TYPE_STRING;
            }
            return PHPExcel_Cell_DataType::TYPE_NUMERIC;
        } elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) {
            return PHPExcel_Cell_DataType::TYPE_ERROR;
        }

        return PHPExcel_Cell_DataType::TYPE_STRING;
    }
}

after that, before you want to call excel function like Excel::import or Excel::load, you must call ::setValueBinder first to overwrite the valuebinder, for the example like this :

use App\Excel\CustomValueBinder;
use Maatwebsite\Excel\Facades\Excel;

public function ProsesExcel(Request $req){
  $path = $req->file('upload_excel')->getRealPath();
  $data = Excel::setValueBinder(new CustomValueBinder())->load($path)->get();
}

problem solved 👍

2 Comments

This "fix" makes no more sense than the original code. It should be is_string($pValue) && $pValue[0] === '=' && strlen($pValue) > 1, the same way as its done couple lines below. And also you must change $tValue{0} to $tValue[0] there. Or ditch that PHPExcel that was discontinued many years ago, and use PHPSpreadsheet instead.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.