PHPExcel匯入、匯出總結
阿新 • • 發佈:2018-12-24
原文 :https://blog.csdn.net/u014236259/article/details/60601767
1、PHPExcel匯出方法實現過程
/** * 資料匯出 * @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 */ function exportExcel($title=array(), $data=array(), $fileName='', $savePath='./', $isDown=false){ include('PHPExcel.php'); $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);
2、PHPExcel匯入方法實現過程
/** * 資料匯入 * @param string $file excel檔案 * @param string $sheet * @return string 返回解析資料 * @throws PHPExcel_Exception * @throws PHPExcel_Reader_Exception */ function importExecl($file = '', $sheet = 0) { $file = iconv("utf-8", "gb2312", $file); //轉碼 if (empty($file) OR !file_exists($file)) { die('file not exists!'); } include('PHPExcel.php'); $objRead = new PHPExcel_Reader_Excel2007(); //建立reader物件 if (!$objRead->canRead($file)) { $objRead = new PHPExcel_Reader_Excel5(); 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; }