PHPExcel使用

1、首先需要引入PHPExcel.php文件,require_once 'libraries/PHPExcel.php';

2、设置时区,date_default_timezone_set('Asia/Shanghai');

3、开始读取

$filePath = 'uploads/step1.xlsx';
     
$PHPExcel = new PHPExcel();
     
$PHPReader = new PHPExcel_Reader_Excel2007();
     
if(!$PHPReader->canRead($filePath)){
    $PHPReader = new PHPExcel_Reader_Excel5();
    if(!$PHPReader->canRead($filePath)){
        echo 'no Excel';
        return ;
    }
}
     
//读取excel
$PHPExcel = $PHPReader->load($filePath);
   
//获取工作表的数目
$sheetCount = $PHPExcel->getSheetCount();
   
$sheet = 0;
   
$currentSheet = $PHPExcel->getSheet($sheet);
$allRow = $currentSheet->getHighestRow(); //获取Excel中信息的行数
$allColumn = $currentSheet->getHighestColumn();//获取Excel的列数
   
//如果列数超过Z,如列数出现了AA,AB。。。,可以使用如下的方法进行转换
//PHPExcel_Cell::columnIndexFromString($allColumn);  //从字符串获取列的索引,AA转成27
//PHPExcel_Cell::stringFromColumnIndex($pColumnIndex); //从索引获取列字符串,27转成AA
//或者首先定义好多少列
$excel_column  = array(
    1 => 'A',
    2 => 'B',
    3 => 'C',
    4 => 'D',
    5 => 'E',
    6 => 'F',
    7 => 'G',
    8 => 'H',
    9 => 'I',
    10 => 'J',
    11 => 'K',
    12 => 'L',
    13 => 'M',
    14 => 'N',
    15 => 'O',
    16 => 'P',
    17 => 'Q',
    18 => 'R',
    19 => 'S',
    20 => 'T',
    21 => 'U',
    22 => 'V',
    23 => 'W',
    24 => 'X',
    25 => 'Y',
    26 => 'Z',
    27 => 'AA',
);
$list = array();
for($currentRow = 1; $currentRow <= $allRow; $currentRow ++){
    $row = array(); 
    foreach ($excel_column as $key => $currentColumn){
        $address = $currentColumn.$currentRow;
        $value = $currentSheet->getCell($address)->getValue();
        $row[] = $value;
    }
    $list[] = $row;
}

 

posted @ 2014-07-02 16:12  1317660800  阅读(222)  评论(0)    收藏  举报