php开发环境升级到php8后,原本基于thinkphp的老项目导入/导出Excel文件,出现各种问题,究其原因:
1、php7.4及以上版本语法上的改变
2、phpoffice已经停止维护更新,很多细节语法已经不支持php7.4及以上版本
新项目请使用PHPExcel替代方案PhpSpreadsheet,老项目可以用文末改好的完整源码
bug 1:
Deprecated: Array and string offset access syntax with curly braces is deprecated in /Users/binzhu/dev/phppro/2023/zambon_2023/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell/DefaultValueBinder.php on line 90
Deprecated: Array and string offset access syntax with curly braces is deprecated in /Users/binzhu/dev/phppro/2023/zambon_2023/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell/DefaultValueBinder.php on line 98
Deprecated: Array and string offset access syntax with curly braces is deprecated in /Users/binzhu/dev/phppro/2023/zambon_2023/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell/DefaultValueBinder.php on line 98
[8] ErrorException in DefaultValueBinder.php line 90
Trying to access array offset on value of type int
iShot_2023-05-06_13.47.40.jpg
修改为如下代码
} elseif (is\_array(`$pValue) && $`pValue\[0] === '=' && strlen(\$pValue) > 1) {
修改后完成代码
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 (is_array($pValue) && $pValue[0] === '=' && 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;
}
bug 2:
牵扯大量文件,都是因为php7.4后不再支持{0}之类语法需要修改为[0]
<b>Deprecated</b>: Array and string offset access syntax with curly braces is deprecated in <b>/Users/binzhu/dev/phppro/2023/zambon_2023/vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/Excel5/Parser.php</b> on line <b>1024</b><br />
iShot_2023-05-06_14.13.15.jpg
改为
for ($i = 0; $i < $col_ref_length; ++$i) {
$col += (ord($col_ref[$i]) - 64) * pow(26, $expn);
--$expn;
}
牵扯这个错误的地方太多太多了,就不一一列举了,改好的源码已经上传你可以直接下载使用
网友评论