php7.4及以上版本,thinkphp中使用phpoffice遇到的问题

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

修改为如下代码

} 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;
}

牵扯这个错误的地方太多太多了,就不一一列举了,改好的源码已经上传你可以直接下载使用

修改后的phpoffice源码下载

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ArslanRobot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值