Excel.class.php
1.99 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?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 文件路径
* @param int $column 获取多少列,默认取得全部 (最多26列)
* @return array
*/
public static function readExcel($filePath, $column = 0)
{
$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);
//取得一共有多少列
if ($column == 0)
{
$allColumn = $currentSheet->getHighestColumn();
}else
{
$column = $column>=26 ? 26 : $column ;
$allColumn = chr(64 + $column);
}
//取得一共有多少行
$allRow = $currentSheet->getHighestRow();
$result = array();
for($currentRow = 1;$currentRow<=$allRow;$currentRow++)
{
$columnNum = 1 ;
for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++)
{
$address = $currentColumn.$currentRow;
$cell = $currentSheet->getCell($address)->getValue();
if($cell instanceof PHPExcel_RichText)
{
//富文本转换
$cell = $cell->__toString();
}
$result[$currentRow][$columnNum] = $cell ;
$columnNum++;
}
}
return $result ;
}
}
?>