首页 > 开发 > PHP > 正文

PHPEXCEL导入excel表格生成数组

2024-05-04 21:48:22
字体:
来源:转载
供稿:网友

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

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表