很簡單就可以實現,下面為大家簡單介紹下
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;