文章主要介紹了PHP獲取excel文件數據的方法。具有很好的參考價值。下面跟着小編一起來看下吧。
1、下載PHPExcel類,是一個文件夾,還得有一個文件,兩個在同級目錄
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require __DIR__ . './PHPExcel/';
$PHPReader = new PHPExcel_Reader_Excel2007();
//判斷文件類型
if (!$PHPReader->canRead($filePath)) {
$PHPReader = new PHPExcel_Reader_Excel5();
if (!$PHPReader->canRead($filePath)) {
echo 'no Excel';
return false;
}
}
$PHPExcel = $PHPReader->load($filePath);
/**讀取excel文件中的`第一個工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列號*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
/**從第1行開始輸出*/
for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
/**從第A列開始輸出*/
for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();
/**ord()將字符轉爲十進制數*/
$date[$currentRow - 1][] = $val;
}
}
return $date;