网站地图    收藏   

主页 > php专栏 > php应用 >

PHPEXCEL导入excel表格生成数组 - php高级应用

来源:自学PHP网    时间:2014-11-27 22:16 作者: 阅读:次

[导读] 本方法使用PHPEXCEL插件读取excel文件转化为数组了,后期还有没有完成的我们可以把转换成数组之后再保存到mysql数据库这个就非常的方便了,代码如下:?php/***@descPHPEXCEL导入*returnarray();*/fu...

PHPEXCEL导入excel表格生成数组

本方法使用PHPEXCEL插件读取excel文件转化为数组了,后期还有没有完成的我们可以把转换成数组之后再保存到mysql数据库这个就非常的方便了,代码如下:

  1. <?php 
  2. /** 
  3.  * @desc PHPEXCEL导入 
  4.  * return array(); 
  5.  */ 
  6. function importExcel($file) 
  7. { 
  8.     require_once 'PHPExcel.php'; 
  9.     require_once 'PHPExcel/IOFactory.php'; 
  10.     require_once 'PHPExcel/Reader/Excel5.php'; 
  11.     $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format 
  12.     $objPHPExcel = $objReader->load($file); 
  13.     $sheet = $objPHPExcel->getSheet(0); 
  14.     $highestRow = $sheet->getHighestRow(); // 取得总行数 
  15.     $highestColumn = $sheet->getHighestColumn(); // 取得总列数 
  16.     $objWorksheet = $objPHPExcel->getActiveSheet(); 
  17.  
  18.     $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); 
  19.     $excelData = array(); 
  20.     for ($row = 1; $row <= $highestRow; $row++) { 
  21.         for ($col = 0; $col < $highestColumnIndex; $col++) { 
  22.             $excelData[$row][] =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); 
  23.         } 
  24.     } 
  25.     return $excelData; 
  26. } 
  27. //用法: 
  28. importExcel('test.xsl'); 
  29. //开源代码phpfensi.com 
  30. ?> 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论