Maison >développement back-end >tutoriel php >php导入及导出excel文件
对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 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教程有兴趣的朋友有所帮助。