导入EXCEL在PHP7.4时发生异常,
Exception-> message()是:
尝试访问类型为int的值的数组偏移量。
预期行为:
当我在php72w上运行代码时,导入一个xlsx文件
实际行为:
当我导入Excel时,将服务器环境从php7.2上传到php7.4之后发生的错误会像上面一样失败。
如果我从php7.4降级到php7.2,将再次恢复正常。
$extension=$request->file()['file']->getClientOriginalExtension();
$extension[0] = strtoupper($extension[0]);
Excel::import(new RejectedImport(), $request->file()['file']->path(),null,$extension);
什么是导入类:
<?php
namespace App\Imports;
use App\Commodity;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class CommodityImport implements ToCollection, WithHeadingRow
{
protected $isOverride=false;
public function __construct($isOverride)
{
if($isOverride=='1')
$this->isOverride=true;
}
/**
* <strong i="26">@param</strong> Collection $collections
*/
public function collection(Collection $collections)
{
foreach ($collections as $row)
{
$barcode = $row['barcode'] ?? $row['BARCODE'] ?? $row['Barcode'];
if(!$barcode)continue;
$name = $row['name'] ?? $row['NAME'] ?? $row['Name'] ?? '';
$sku = $row['sku'] ?? $row['SKU'] ?? $row['Sku'] ?? '';
$owner = $row['owner'] ?? $row['owner_name'] ?? $row['OWNER'] ?? $row['Owner'] ?? '';
$commodity=Commodity::where('barcode',$row['barcode'])->first();
if($commodity){
if($this->isOverride){
$name?$commodity['name']= $name:false;
$sku?$commodity['sku']= $sku:false;
$owner?$commodity['owner_name']= $owner:false;
$commodity->update();
}
}else{
Commodity::create([
'name' => $name,
'sku' => $sku,
'owner_name' => $owner,
'barcode' => $barcode,
]);
}
}
}
}
感谢您提交票证。 不幸的是,您提供的信息不完整。 我们需要知道您使用哪个版本以及如何复制它。 请包括代码示例。 在我们拿起它之前,请检查(https://github.com/Maatwebsite/Laravel-Excel/blob/3.1/.github/ISSUE_TEMPLATE.md)并添加缺少的信息。 为了简化此票证的处理,请确保检查(https://laravel-excel.maatwebsite.nl/3.1/getting-started/contributing.html)并再次检查是否已填写问题模板正确地。 这将使我们能够更有效地接您的票。 正确遵循准则的问题将优先于其他问题。
您能显示更多的堆栈跟踪信息吗,我无法在PHP7.4上使用该软件包时确认该错误。 也许这是您自己的代码中的东西?
嗨@patrickbrouwers谢谢你的包裹,我是它的忠实粉丝!
升级后,尽管在导出过程中,我也遇到了类似的错误,并且堆栈跟踪表明问题出在phpsreadsheet上,而不是Laravel-Excel,但是google将我带到了这里。
这是堆栈跟踪:
#0 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/DefaultValueBinder.php(56): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/DefaultValueBinder.php(34): PhpOffice\\PhpSpreadsheet\\Cell\\DefaultValueBinder::dataTypeForValue()
#2 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php(184): PhpOffice\\PhpSpreadsheet\\Cell\\DefaultValueBinder->bindValue()
#3 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Worksheet.php(2510): PhpOffice\\PhpSpreadsheet\\Cell\\Cell->setValue()
#4 /vendor/maatwebsite/excel/src/Sheet.php(402): PhpOffice\\PhpSpreadsheet\\Worksheet\\Worksheet->fromArray()
#5 /vendor/maatwebsite/excel/src/Sheet.php(502): Maatwebsite\\Excel\\Sheet->append()
#6 /vendor/maatwebsite/excel/src/Sheet.php(368): Maatwebsite\\Excel\\Sheet->appendRows()
#7 /vendor/maatwebsite/excel/src/Sheet.php(195): Maatwebsite\\Excel\\Sheet->fromCollection()
#8 /vendor/maatwebsite/excel/src/Writer.php(73): Maatwebsite\\Excel\\Sheet->export()
#9 /vendor/maatwebsite/excel/src/Excel.php(176): Maatwebsite\\Excel\\Writer->export()
#10 /vendor/maatwebsite/excel/src/Excel.php(97): Maatwebsite\\Excel\\Excel->export()
#11 /app/Console/Commands/EmailCardRequestList.php(89): Maatwebsite\\Excel\\Excel->store()
已在PhpSpreadsheet中修复,请参见https://github.com/PHPOffice/PhpSpreadsheet/issues/1300
在PHPExcel文件“ DefaultValueBinder.php”中,替换此行82:
} elseif($ pValue [0] ==='='&& strlen($ pValue)> 1){
具有以下内容:
} elseif(0 === strpos($ pValue,'=')&& strlen($ pValue)> 1){
这将修复“尝试访问类型为int的值的数组偏移量”错误。 但是您还需要在整个PHPExcel代码中进行查找和替换花括号,以解决“不建议使用不使用花括号的数组和字符串偏移量访问语法”错误,该错误也影响PHP升级到7.4。 我只是到处都用“ [”和“]”替换了它们,一切又恢复正常了。
供应商文件夹内的DefaultValueBinder.php? 还是在哪里? 这里同样的问题。
供应商文件夹内的DefaultValueBinder.php? 还是在哪里? 这里同样的问题。
我很抱歉。 我应该更具体一些。 在我的安装中,它位于以下位置的PHPExcel文件夹中:PHPExcel \ Cell \ DefaultValueBinder.php
它可能会因您的安装而异。 但是您应该能够在系统上对其进行搜索。
我认为这行是62,而不是我的朋友82! 我希望它能奏效,因为我会感到压力很大。 我在项目中找不到错误的解决方案:错误错误:尝试访问类型为int'/ Cell / DefaultValueBinder的值的数组偏移量。 php:56 '。 我寄予厚望!
这是路径:third_party / PHPExcel / Classes / PHPExcel / Cell / DefaultValueBinder.php
这里的third_party表示您保存PHPExcel文件夹的位置
现在搜索==='='并检查是否有匹配的行,然后将其替换为
0 === strpos($ pValue,'=')
休息会是一样的。 就我而言
$ pValue [0] ==='='
在PHPExcel文件“ DefaultValueBinder.php”中,替换此行82:
} elseif($ pValue [0] ==='='&& strlen($ pValue)> 1){
具有以下内容:
} elseif(0 === strpos($ pValue,'=')&& strlen($ pValue)> 1){
这将修复“尝试访问类型为int的值的数组偏移量”错误。 但是您还需要在整个PHPExcel代码中进行查找和替换花括号,以解决“不建议使用不使用花括号的数组和字符串偏移量访问语法”错误,该错误也影响PHP升级到7.4。 我只是到处都用“ [”和“]”替换了它们,一切又恢复正常了。
有用! 爱你!
在PHPExcel文件“ DefaultValueBinder.php”中,替换此行82:
} elseif($ pValue [0] ==='='&& strlen($ pValue)> 1){
具有以下内容:
} elseif(0 === strpos($ pValue,'=')&& strlen($ pValue)> 1){
这将修复“尝试访问类型为int的值的数组偏移量”错误。 但是您还需要在整个PHPExcel代码中进行查找和替换花括号,以解决“不建议使用不使用花括号的数组和字符串偏移量访问语法”错误,该错误也影响PHP升级到7.4。 我只是到处都用“ [”和“]”替换了它们,一切又恢复正常了。
当我几乎降级到PHP 7.3时,谢谢,这对我有所帮助
有什么想法可以使用PHP 7.4.3和maatwebsite / excel 2.1版解决此问题吗?
您将必须分叉已弃用的phpexcel软件包,并自己维护该分叉。
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
布尔斯特里斯,拉力?
在检查完is_bool和is_float之后,只需将这段代码放入即可
代码不是此程序包的一部分,而是旧版PhpExcel的一部分
在PHPExcel文件“ DefaultValueBinder.php”中,替换此行82:
} elseif($ pValue [0] ==='='&& strlen($ pValue)> 1){
具有以下内容:
} elseif(0 === strpos($ pValue,'=')&& strlen($ pValue)> 1){
这将修复“尝试访问类型为int的值的数组偏移量”错误。 但是您还需要在整个PHPExcel代码中进行查找和替换花括号,以解决“不建议使用不使用花括号的数组和字符串偏移量访问语法”错误,该错误也影响PHP升级到7.4。 我只是到处都用“ [”和“]”替换了它们,一切又恢复正常了。
谢谢你,兄弟。 爱它。 Thankx @burello <3
最好的办法是在项目中将PHPOffice/PhpSpreadsheet
更新到1.10以上-因为这样一来,此错误将得到解决,而无需手动编辑程序包代码库。 所以对我来说
composer require phpoffice/phpspreadsheet "^1.10"
解决了问题。
我也有这个errrror
已经一无所知
在PHPExcel文件“ DefaultValueBinder.php”中,替换此行82:
} elseif($ pValue [0] ==='='&& strlen($ pValue)> 1){
具有以下内容:
} elseif(0 === strpos($ pValue,'=')&& strlen($ pValue)> 1){
这将修复“尝试访问类型为int的值的数组偏移量”错误。 但是您还需要在整个PHPExcel代码中进行查找和替换花括号,以解决“不建议使用不使用花括号的数组和字符串偏移量访问语法”错误,该错误也影响PHP升级到7.4。 我只是到处都用“ [”和“]”替换了它们,一切又恢复正常了。
谢谢为我工作! orderimportexport.ocmod opencart 3x
在PHPExcel文件“ DefaultValueBinder.php”中,替换此行82:
} elseif($ pValue [0] ==='='&& strlen($ pValue)> 1){
具有以下内容:
} elseif(0 === strpos($ pValue,'=')&& strlen($ pValue)> 1){
这将修复“尝试访问类型为int的值的数组偏移量”错误。 但是您还需要在整个PHPExcel代码中进行查找和替换花括号,以解决“不建议使用不使用花括号的数组和字符串偏移量访问语法”错误,该错误也影响PHP升级到7.4。 我只是到处都用“ [”和“]”替换了它们,一切又恢复正常了。
太神奇了,您救了我兄弟,非常感谢
替换此行82:
} elseif($ pValue [0] ==='='&& strlen($ pValue)> 1){
具有以下内容:
} elseif(0 === strpos($ pValue,'=')&& strlen($ pValue)> 1){
为我工作...
请向PhpSpreadsheet报告PhpSpreadsheet中的错误。
最有用的评论
在PHPExcel文件“ DefaultValueBinder.php”中,替换此行82:
} elseif($ pValue [0] ==='='&& strlen($ pValue)> 1){
具有以下内容:
} elseif(0 === strpos($ pValue,'=')&& strlen($ pValue)> 1){
这将修复“尝试访问类型为int的值的数组偏移量”错误。 但是您还需要在整个PHPExcel代码中进行查找和替换花括号,以解决“不建议使用不使用花括号的数组和字符串偏移量访问语法”错误,该错误也影响PHP升级到7.4。 我只是到处都用“ [”和“]”替换了它们,一切又恢复正常了。