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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM