php開發(fā)環(huán)境升級到php8后笔链,原本基于thinkphp的老項目導(dǎo)入/導(dǎo)出Excel文件吼蚁,出現(xiàn)各種問題,究其原因:
1宁否、php7.4及以上版本語法上的改變
2、phpoffice已經(jīng)停止維護更新缀遍,很多細節(jié)語法已經(jīng)不支持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
修改為如下代碼
} 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 />
改為
for ($i = 0; $i < $col_ref_length; ++$i) {
$col += (ord($col_ref[$i]) - 64) * pow(26, $expn);
--$expn;
}
牽扯這個錯誤的地方太多太多了域醇,就不一一列舉了台谊,改好的源碼已經(jīng)上傳你可以直接下載使用