對excel表格進行操作,要用到PHPExcel外掛程式。由於我一般都用的是CI架構,以CI架構來介紹。
首先要引入PHPExcel的相關檔案:
$this->load->library('PHPExcel/IOFactory');$this->load->model('search_model');接下來介紹怎麼匯出excel檔案,首先要建立工作表,並對它進行初始化:
//建立工作表$objPHPExcel = new PHPExcel();// 操作第一個工作表 $objPHPExcel->setActiveSheetIndex(0); // 設定工作薄名稱 $objPHPExcel->getActiveSheet()->setTitle("集合"); // 設定預設字型和大小 $objPHPExcel->getDefaultStyle()->getFont()->setName("宋體"); $objPHPExcel->getDefaultStyle()->getFont()->setSize(10); 然後設定表頭,當然也可以不設定,setCellValueByColumnAndRow($col,$row,$name)方法第一個參數是操作的列(起始是0),第二個參數是操作的行(起始是1),第三個參數是你要設定的值:
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 1, " ");$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 1, "IMSI");$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, 1, "IMEI");$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, 1, "裝置號");$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, 1, "號段");$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, 1, "歸屬地");$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, 1, "電訊廠商");$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, 1, "時間");
設定好表頭後,就開始逐行的賦值了,操作方法同上:
$row = 2;$i = 0;foreach($data as $item){$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $row, $i+1);$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $row, $item['QQ']);$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $row, $item['MSN']);$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $row, $item['DeviceID']);$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $row, $item['RTX']);$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $row, $item['Area']);$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $row, $item['Type']);$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $row, $item['TimeStamp']);$row++;$i++;}最後收尾,將檔案輸出到頁面,提供下載:
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); ob_end_clean();header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type: application/vnd.ms-excel;charset=UTF-8"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header("Content-Disposition:attachment;filename=result.xls"); //定義檔案名稱 header("Content-Transfer-Encoding:binary"); $objWriter->save("php://output");
匯入方法也與之類似,首先將檔案上傳到後台,擷取路徑然後開啟excel:
$orig_name = explode('.',$upload_data['orig_name'])[0];//原始檔案名$extend=strrchr ($upload_data['file_name'],'.'); //擷取檔案類型$readerType = ($extend == ".xlsx") ? "Excel2007" : "Excel5";$path = $config['upload_path'].$upload_data['file_name']; //檔案路徑$objPHPExcel = new IOFactory();$objReader = $objPHPExcel::createReader($readerType)->load($path);$sheet = $objReader->getSheet( 0 );
然後迴圈讀取資料,入庫:
$highestRow = $sheet->getHighestRow(); // 取得總行數 但是很多無資料的空白行也讀取了,所以未採用此方法$highestColumn = $sheet->getHighestColumn(); // 取得總列數$colspan = range( 'A', $highestColumn );$rowArray = $sheet->getRowDimensions();// 取得總行數 $rowCount = count($rowArray);//迴圈讀取excel檔案(因為第一行我一般都加了表頭,所以總是從第二行開始讀資料)for ( $j = 2; $j <= $rowCount; $j++ ) {$array = array( );foreach ( $colspan as $value ) {$array[] = $objReader->getActiveSheet()->getCell( $value . $j )->getValue();}//插入表中$sql = "insert into im_blacklist(`name`,`MAC`,`IMSI`,`IMEI`,`groupid`) values('".$array[1]."','".$array[2]."',".$array[3].",".$array[4].",".$id.")";$this->db->query($sql);}但是此方法讀取時跟excel游標所在行有關聯,最好開啟excel將游標移動到表格右下角,再關閉excel進行讀操作,這時才能正確讀取到所有資料。
以上就介紹了php匯入及匯出excel檔案,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。