Excel.class.php
1.6 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
if (!class_exists('PHPExcel'))
{
require_once dirname(__FILE__).'/PHPExcel.php';
}
class Util_Excel_Excel extends PHPExcel
{
private static $_phpexcel = null ;
/**
* 获取Excel实例
*
* @return unknown
*/
public static function getInstance()
{
if (self::$_phpexcel == null)
{
self::$_phpexcel = new self() ;
}
return self::$_phpexcel;
}
/**
* 读取Excel内容
*
* @param string $filePath 文件路径
* @return array
*/
public static function readExcel($filePath)
{
$PHPExcel = self::getInstance();
$PHPReader = new PHPExcel_Reader_Excel2007(); //用于2007版本
if(!$PHPReader->canRead($filePath))
{
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath))
{
//无法再载入该文件,返回false
return false ;
}
}
$PHPExcel = $PHPReader->load($filePath);
$currentSheet = $PHPExcel->getSheet(0);
//取得一共有多少列
$allColumn = $currentSheet->getHighestColumn();
//取得一共有多少行
$allRow = $currentSheet->getHighestRow();
$result = array();
for($currentRow = 1;$currentRow<=$allRow;$currentRow++)
{
$columnNum = 1 ;
for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++)
{
$address = $currentColumn.$currentRow;
$result[$currentRow][$columnNum] = $currentSheet->getCell($address)->getValue();
$columnNum++;
}
}
return $result ;
}
}
?>