首页 / PHP / PHPExcel读取excel文件问题
PHPExcel读取excel文件问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHPExcel读取excel文件问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6999字,纯文字阅读大概需要10分钟。
内容图文
读取代码如下:$uploadfile=$_FILES["file"]['tmp_name']; if(is_uploaded_file($uploadfile) && $_POST['aid']){ Vendor('PhpExcel.PHPExcel'); Vendor('PHPExcel.IOFactory'); Vendor('PHPExcel.Reader.Excel5'); $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format $objPHPExcel = $objReader->load($uploadfile); $sheet = $objPHPExcel->getSheet(0); $highestRow = 0; // 取得总行数 $highestColumn = $sheet->getHighestColumn(); // 取得总列数 $start=1;//从哪一行开始录入/删除 if($_POST['diyihang']){ //忽略第一行 $highestRow = $sheet->getHighestRow(); // 取得总行数 $start=2; }else{ $highestRow= ($sheet->getHighestRow()); // 取得总行数 } $update=array(); $delete=array(); //获取信息字段 $fresult=M("form")->where("aid = %d",$_POST['aid'])->order("sort")->select(); for($start;$start<=$highestRow;$start++){ //读取单元格 $temp['title']=$objPHPExcel->getActiveSheet()->getCell("B".$start)->getValue(); //物业编号,认证码为空 不要 if(empty($temp['title'])){ continue; } $temp['assess']=$objPHPExcel->getActiveSheet()->getCell("C".$start)->getValue(); $temp['is_status']=$objPHPExcel->getActiveSheet()->getCell("D".$start)->getValue(); $temp['sort']=$objPHPExcel->getActiveSheet()->getCell("E".$start)->getValue(); $temp['aid']=$_POST['aid']; $coll=ord("F"); foreach($fresult as $key=>$one){ $temp['form_content'][$one['id']]=$objPHPExcel->getActiveSheet()->getCell(chr($coll).$start)->getValue(); $coll++; } $temp['form_content']=serialize($temp['form_content']); if($objPHPExcel->getActiveSheet()->getCell("A".$start)->getValue()){ $temp['id']=$objPHPExcel->getActiveSheet()->getCell("A".$start)->getValue(); array_push($delete, $temp); }else{ array_push($update, $temp); } unset($temp); }
这里不明白为什么时好时坏,好的时候可以正常输出数组数据,坏的时候输出的数据如下:
array(2) { [0]=> array(6) { ["title"]=> object(PHPExcel_RichText)#223 (1) { ["_richTextElements":"PHPExcel_RichText":private]=> array(2) { [0]=> object(PHPExcel_RichText_Run)#224 (2) { ["_font":"PHPExcel_RichText_Run":private]=> object(PHPExcel_Style_Font)#227 (12) { ["_name":protected]=> string(6) "瀹嬩綋" ["_size":protected]=> int(11) ["_bold":protected]=> bool(false) ["_italic":protected]=> bool(false) ["_superScript":protected]=> bool(false) ["_subScript":protected]=> bool(false) ["_underline":protected]=> string(4) "none" ["_strikethrough":protected]=> bool(false) ["_color":protected]=> object(PHPExcel_Style_Color)#228 (4) { ["_argb":protected]=> string(8) "FF000000" ["_parentPropertyName":protected]=> NULL ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL } ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL ["colorIndex"]=> int(8) } ["_text":"PHPExcel_RichText_TextElement":private]=> string(6) "椤烘櫙" } [1]=> object(PHPExcel_RichText_Run)#225 (2) { ["_font":"PHPExcel_RichText_Run":private]=> object(PHPExcel_Style_Font)#230 (12) { ["_name":protected]=> string(7) "Calibri" ["_size":protected]=> int(11) ["_bold":protected]=> bool(false) ["_italic":protected]=> bool(false) ["_superScript":protected]=> bool(false) ["_subScript":protected]=> bool(false) ["_underline":protected]=> string(4) "none" ["_strikethrough":protected]=> bool(false) ["_color":protected]=> object(PHPExcel_Style_Color)#231 (4) { ["_argb":protected]=> string(8) "FF000000" ["_parentPropertyName":protected]=> NULL ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL } ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL ["colorIndex"]=> int(8) } ["_text":"PHPExcel_RichText_TextElement":private]=> string(5) "1-502" } } } ["assess"]=> string(6) "宸蹭氦" ["is_status"]=> float(1) ["sort"]=> float(1000) ["aid"]=> string(3) "692" ["form_content"]=> string(32) "a:2:{i:829;d:123;i:830;d:42225;}" } [1]=> array(6) { ["title"]=> object(PHPExcel_RichText)#236 (1) { ["_richTextElements":"PHPExcel_RichText":private]=> array(2) { [0]=> object(PHPExcel_RichText_Run)#237 (2) { ["_font":"PHPExcel_RichText_Run":private]=> object(PHPExcel_Style_Font)#240 (12) { ["_name":protected]=> string(6) "瀹嬩綋" ["_size":protected]=> int(11) ["_bold":protected]=> bool(false) ["_italic":protected]=> bool(false) ["_superScript":protected]=> bool(false) ["_subScript":protected]=> bool(false) ["_underline":protected]=> string(4) "none" ["_strikethrough":protected]=> bool(false) ["_color":protected]=> object(PHPExcel_Style_Color)#241 (4) { ["_argb":protected]=> string(8) "FF000000" ["_parentPropertyName":protected]=> NULL ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL } ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL ["colorIndex"]=> int(8) } ["_text":"PHPExcel_RichText_TextElement":private]=> string(6) "椤烘櫙" } [1]=> object(PHPExcel_RichText_Run)#238 (2) { ["_font":"PHPExcel_RichText_Run":private]=> object(PHPExcel_Style_Font)#243 (12) { ["_name":protected]=> string(7) "Calibri" ["_size":protected]=> int(11) ["_bold":protected]=> bool(false) ["_italic":protected]=> bool(false) ["_superScript":protected]=> bool(false) ["_subScript":protected]=> bool(false) ["_underline":protected]=> string(4) "none" ["_strikethrough":protected]=> bool(false) ["_color":protected]=> object(PHPExcel_Style_Color)#244 (4) { ["_argb":protected]=> string(8) "FF000000" ["_parentPropertyName":protected]=> NULL ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL } ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL ["colorIndex"]=> int(8) } ["_text":"PHPExcel_RichText_TextElement":private]=> string(5) "1-503" } } } ["assess"]=> string(6) "鏈浜" ["is_status"]=> float(1) ["sort"]=> float(1001) ["aid"]=> string(3) "692" ["form_content"]=> string(32) "a:2:{i:829;d:125;i:830;d:42225;}" } }
我想知道为什么会输出如上的数据格式,为什么会带有样式
回复讨论(解决方案)
没有大神解答一下么?
问题都没描述全,时好时坏是什么情况?只传同一个文件还是不同的?不同的文件,有没有对比格式?xls还是xlsx?同样格式的文件有好有坏,有没有打开文件对比内容?
问题都没描述全,时好时坏是什么情况?只传同一个文件还是不同的?不同的文件,有没有对比格式?xls还是xlsx?同样格式的文件有好有坏,有没有打开文件对比内容?
文件格式为xls,时好时坏是相同的文件,改变一下其中的内容,就无法导入,获取的数据带有excel的一些属性。文件的内容肯定是不相同的,但是都是正确数据。而且文件的格式也是正确的。
问题解决了,强制转换一下类型就可以了 (string)
//循环读取每个单元格的内容。注意行从1开始,列从A开始
for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){
for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
$addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
if($cell instanceof PHPExcel_RichText) //富文本转换字符串
$cell = $cell->__toString();
echo $cell;
}
}
内容总结
以上是互联网集市为您收集整理的PHPExcel读取excel文件问题全部内容,希望文章能够帮你解决PHPExcel读取excel文件问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。