Excel.class.php 1.6 KB
<?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 ;
    	
    }
}
?>