用phpexcel解析上传的excel

1,925次阅读
没有评论
$tp = array("application/octet-stream","text/html","application/msexcel","application/vnd.ms-excel");
		if(!in_array($_FILES["img"]["type"],$tp)){
			dieexcel('所上传的EXCEL文档无效,错误格式:'.$_FILES["img"]["type"]);
		}else{
			$file1 = "excel/".$_FILES["img"]["name"];
			move_uploaded_file($_FILES["img"]["tmp_name"],$file1);
			
			$objReader = PHPExcel_IOFactory::createReader('Excel5');
			$objReader->setReadDataOnly(true);
			$objPHPExcel = $objReader->load($file1);
			$objWorksheet = $objPHPExcel->getActiveSheet();
			$highestRow = $objWorksheet->getHighestRow(); //

			$highestColumn = $objWorksheet->getHighestColumn();
			
			$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // 		

			for($currentRow=2;$currentRow<=$highestRow;$currentRow++)
			{

					$b_class= $objWorksheet->getCell('A'.$currentRow)->getValue(); 
					$s_class= $objWorksheet->getCell('B'.$currentRow)->getValue(); 
					$no= $objWorksheet->getCell('C'.$currentRow)->getValue(); 
					$name= $objWorksheet->getCell('D'.$currentRow)->getValue(); 
					$num= $objWorksheet->getCell('E'.$currentRow)->getValue(); 
					$level= $objWorksheet->getCell('F'.$currentRow)->getValue(); 
					$rarity= $objWorksheet->getCell('G'.$currentRow)->getValue(); 
					$price= $objWorksheet->getCell('H'.$currentRow)->getValue(); 
					$content= $objWorksheet->getCell('I'.$currentRow)->getValue(); 
		
				
				//判断
				
				
				$ismenu = true;
				$bname = Get_Rewrite_Url($b_class);				
				//大类处理
				if ($bname!='' and $b_name!=$bname){
					$sql = "select fid from t_menu where fclass='$nclass' and fgourl='$bname'";
					$result = $db->query($sql);
					if ($rs = $result->fetch()){
						$b_class = $rs["fid"];
						$class = $rs["fid"];
						$b_name = $bname;
					}else{
						$sql = "insert into t_menu (fname_en,fisarea,fclass,forderid,fgourl,ftype) ";
						$sql .= "values('".$b_class."','2','$nclass',0,'$bname','product')";
						if(mysql_query($sql)){
							$b_class =  mysql_insert_id();
							$class =  mysql_insert_id();
							$b_name = $bname;
						}else{
							$ismenu = false;
						}
					}
				}elseif($bname==''){
					$ismenu = false;
				}
				
				//小类处理
				if ($ismenu){
					$sname = Get_Rewrite_Url($s_class);
					if ($sname!='' and $s_name!=$sname){
						$sql = "select fid from t_menu where fclass='$b_class' and fgourl='$sname'";
						$result = $db->query($sql);
						if ($rs = $result->fetch()){
							$s_class = $rs["fid"];
							$class = $rs["fid"];
							$s_name = $bname;
						}else{
							$sql = "insert into t_menu (fname_en,fisarea,fclass,forderid,fgourl,ftype) ";
							$sql .= "values('".$s_class."','3','$b_class',0,'$sname','product')";
							if(mysql_query($sql)){
								$s_class =  mysql_insert_id();
								$class =  mysql_insert_id();
								$s_name = $bname;
							}else{
								$ismenu = false;
							}
						}
					}
				}
正文完
 

公众号