首先需要去官網https://github.com/phpOffice/PHPExcel/下載PHPExcel,下載后只需要Classes目錄下的文件即可。
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('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'); //引入PHP EXCEL類 $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(); if($cellValue instanceof PHPExcel_RichText){ //富文本轉換字符串 $cellValue = $cellValue->__toString(); } $data[$_row][$cellName[$_column]] = $cellValue; } } return $data;}
新聞熱點
疑難解答
圖片精選