欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > php >内容正文

php

php使用office modi吗,使用phpoffice/phpexcel拓展读取excel内容卡死解决 -凯发k8官方网

发布时间:2024/10/8 php 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 php使用office modi吗,使用phpoffice/phpexcel拓展读取excel内容卡死解决 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

今天一个线上项目反应导入excel文件时,网页直接卡死,导入excel文件时失败,经过一步一步的验证发现是在读取excel文件时卡死,此项目使用的是phpoffice/phpexcel拓展,最后发现是在执行load方法加载excel文件时出现卡死,之前在导入excel文件一直是正常的,而且excel中的数据也不是很多,到底是由于什么原因造成的呢?

最后在查找phpoffice/phpexcel官方文档中发现,这是由于excel文件中存在了某些特殊字符导致了加载excel文件时崩溃,既然找到了原因,就一定有解决方法,在phpoffice/phpexcel中有一个属性可以解决此方法readdataonly,此属性在拓展中默认时false,我们需要将readdataonly属性设置为true$phpexcel->setreaddataonly(true);  //过滤excel中的特殊字符

所以完整的读取excel的代码如下:$phpexcel = new phpexcel_reader_excel2007();

$file = 'excel文件';

try {

if (!$phpexcel->canread($file)) {

//默认用excel2007读取excel,若格式不对,则用之前的版本进行读取

$phpexcel = new phpexcel_reader_excel5();

}

$phpexcel->setreaddataonly(true);  //过滤excel中的特殊字符

$excel = $phpexcel->load($file);//加载excel

//读取excel文件中的第一个工作表

$currentsheet = $excel->getsheet(0);

} catch (exception $e) {

throw new invalidvalueexception('请选择正确的导入文件!');

}

$allrow = $currentsheet->gethighestrow() - 1; //获取excel总行数

//读取excel信息

for ($currentrow = 1; $currentrow <= $allrow; $currentrow ) {

//读取指定列数据0表示第一列

$currentsheet->getcellbycolumnandrow(0, $currentrow 1)->getvalue()

}

总结

以上是凯发k8官方网为你收集整理的php使用office modi吗,使用phpoffice/phpexcel拓展读取excel内容卡死解决的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图