php获取excel文件数据

很简单就可以实现,下面为大家简单介绍下

1、下载PHPExcel类,是一个文件夹,还得有一个文件PHPExcel.php,两个在同级目录

 

 1 require __DIR__ . './PHPExcel/IOFactory.php';
 2 
 3         $PHPReader = new \PHPExcel_Reader_Excel2007();
 4 
 5         //判断文件类型
 6         if (!$PHPReader->canRead($filePath)) {
 7             $PHPReader = new \PHPExcel_Reader_Excel5();
 8 
 9             if (!$PHPReader->canRead($filePath)) {
10                 echo 'no Excel';
11                 return false;
12             }
13         }
14 
15         $PHPExcel = $PHPReader->load($filePath);
16         /**读取excel文件中的第一个工作表*/
17 
18         $currentSheet = $PHPExcel->getSheet(0);
19         /**取得最大的列号*/
20 
21         $allColumn = $currentSheet->getHighestColumn();
22         /**取得一共有多少行*/
23 
24         $allRow = $currentSheet->getHighestRow();
25 
26         /**从第1行开始输出*/
27         for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
28 
29             /**从第A列开始输出*/
30             for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
31                 $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();
32                 /**ord()将字符转为十进制数*/
33                 $date[$currentRow - 1][] = $val;
34             }
35 
36         }
37         return $date;

 

posted @ 2017-04-20 17:56  阡尘小陌  阅读(3045)  评论(0编辑  收藏  举报