天天看點

phpExcel導入導出

//excel導入資料

public function importExecl( sheet∗@returnstring傳回解析資料∗@throwsPHPExcelE​xception∗@throwsPHPExcelR​eaderE​xception∗/publicfunctionimportExecl(file=’’, $sheet=0){

$file = iconv(“utf-8”, “gb2312”, f i l e ) ; / / 轉 碼 i f ( e m p t y ( file); //轉碼 if(empty( file);//轉碼if(empty(file) OR !file_exists($file)) {

die(‘file not exists!’);

}

Vendor(‘PHPExcel.PHPExcel’); //引入PHP EXCEL類

o b j R e a d = n e w P H P E x c e l R e a d e r E x c e l 2007 ( ) ; / / 建 立 r e a d e r 對 象 i f ( ! objRead = new PHPExcel_Reader_Excel2007(); //建立reader對象 if(! objRead=newPHPExcelR​eaderE​xcel2007();//建立reader對象if(!objRead->canRead($file)){

o b j R e a d = n e w P H P E x c e l R e a d e r E x c e l 5 ( ) ; i f ( ! objRead = new PHPExcel_Reader_Excel5(); if(! objRead=newPHPExcelR​eaderE​xcel5();if(!objRead->canRead($file)){

die(‘No Excel!’);

}

}

$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');  
  
    $obj = $objRead->load($file);  //建立excel對象  
    $currSheet = $obj->getSheet($sheet);   //擷取指定的sheet表  
    $columnH = $currSheet->getHighestColumn();   //取得最大的列号  
    $columnCnt = array_search($columnH, $cellName);  
    $rowCnt = $currSheet->getHighestRow();   //擷取總行數  
  
    $data = array();  
    for($_row=1; $_row<=$rowCnt; $_row++){  //讀取内容  
        for($_column=0; $_column<=$columnCnt; $_column++){  
            $cellId = $cellName[$_column].$_row;  
            $cellValue = $currSheet->getCell($cellId)->getValue();  
             //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue();  #擷取公式計算的值  
            if($cellValue instanceof PHPExcel_RichText){   //富文本轉換字元串  
                $cellValue = $cellValue->__toString();  
            }  
  
            $data[$_row][$cellName[$_column]] = $cellValue;  
        }  
    }  
  
    return $data;  
}  
//導出資料excel
/** 
 * 資料導出 
 * @param array $title   标題行名稱 
 * @param array $data   導出資料 
 * @param string $fileName 檔案名 
 * @param string $savePath 儲存路徑 
 * @param $type   是否下載下傳  false--儲存   true--下載下傳 
 * @return string   傳回檔案全路徑 
 * @throws PHPExcel_Exception 
 * @throws PHPExcel_Reader_Exception 
 */  
public function exportExcel($title=array(), $data=array(), $fileName='', $savePath='./', $isDown=false){  
    Vendor('PHPExcel.PHPExcel');  
    $obj = new PHPExcel();  
  
    //橫向單元格辨別  
    $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');  
      
    $obj->getActiveSheet(0)->setTitle('sheet名稱');   //設定sheet名稱  
    $_row = 1;   //設定縱向單元格辨別  
    if($title){  
        $_cnt = count($title);  
        $obj->getActiveSheet(0)->mergeCells('A'.$_row.':'.$cellName[$_cnt-1].$_row);   //合并單元格  
        $obj->setActiveSheetIndex(0)->setCellValue('A'.$_row, '資料導出:'.date('Y-m-d H:i:s'));  //設定合并後的單元格内容  
        $_row++;  
        $i = 0;  
        foreach($title AS $v){   //設定列标題  
            $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);  
            $i++;  
        }  
        $_row++;  
    }  
  
    //填寫資料  
    if($data){  
        $i = 0;  
        foreach($data AS $_v){  
            $j = 0;  
            foreach($_v AS $_cell){  
                $obj->getActiveSheet(0)->setCellValue($cellName[$j] . ($i+$_row), $_cell);  
                $j++;  
            }  
            $i++;  
        }  
    }  
      
    //檔案名處理  
    if(!$fileName){  
        $fileName = uniqid(time(),true);  
    }  
  
    $objWrite = PHPExcel_IOFactory::createWriter($obj, 'Excel2007');  
  
    if($isDown){   //網頁下載下傳  
        header('pragma:public');  
        header("Content-Disposition:attachment;filename=$fileName.xls");  
        $objWrite->save('php://output');exit;  
    }  
  
    $_fileName = iconv("utf-8", "gb2312", $fileName);   //轉碼  
    $_savePath = $savePath.$_fileName.'.xlsx';  
     $objWrite->save($_savePath);  
  
     return $savePath.$fileName.'.xlsx';  
}  
//exportExcel(array('姓名','年齡'),array(array('a',21),array('b',23)), '檔案', './', true);