网站首页 语言 会计 电脑 医学 资格证 职场 文艺体育 范文
当前位置:书香门第 > 计算机 > php语言

如何使用php获取excel文件数据

栏目: php语言 / 发布于: / 人气:8.28K

文章主要介绍了PHP获取excel文件数据的方法。具有很好的参考价值。下面跟着小编一起来看下吧。

如何使用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;