当前位置:首页 > PHP教程 > php高级应用 > 列表

PHPEXCEL导入excel表格生成数组

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-26 11:11:43 浏览: 评论:0 

本方法使用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. ?> 

Tags: PHPEXCEL导入 excel表格数组

分享到: