php excel导入

<?php
date_default_timezone_set("prc");
if (
($_FILES["foot"]["size"] < 20000)&&($_FILES["foot"]["type"]=="application/octet-stream" ))
  {
  if ($_FILES["foot"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["foot"]["error"] . "<br />";
    }
  else
    {
  //  echo "Upload: " . $_FILES["foot"]["name"] . "<br />";
   // echo "Type: " . $_FILES["foot"]["type"] . "<br />";
  //  echo "Size: " . ($_FILES["foot"]["size"] / 1024) . " Kb<br />";
   // echo "Temp file: " . $_FILES["foot"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["foot"]["name"]))
      {
      echo $_FILES["foot"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["foot"]["tmp_name"],
      "upload/" . $_FILES["foot"]["name"]);
   require_once 'reader.php';
   require_once "oleread.inc";
   $data=new Spreadsheet_Excel_Reader();
   $data->setOutputEncoding("utf-8");
   $data->read( "upload/" . $_FILES["foot"]["name"]);
     $conn=mysql_connect('localhost','root','123456')or die("连接失败".mysql_errno());
				
				
				mysql_select_db("stu",$conn)or die("连接数据库失败".mysql_errno());
			
				mysql_query("set names 'gbk'");
 $m=new match();
   for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {  //将execl数据插入数据库  $i表示从excel的第$i行开始读取  
                   $nameteamA=$data->sheets[0]['cells'][$i][1];
                   $nameteamA=iconv("utf-8","gbk",$nameteamA);
                   $idteamA=$m->findteam($nameteamA);
                   $nameteamB=$data->sheets[0]['cells'][$i][2];
                     $nameteamB=iconv("utf-8","gbk",$nameteamB);
                    $idteamB=$m->findteam($nameteamB);
                    $scoreA=$data->sheets[0]['cells'][$i][3];
                     $scoreB=$data->sheets[0]['cells'][$i][4];
                     if($scoreA>$scoreB){
                     	$result=1;
                     }
                     if($scoreA<$scoreB){
                     	$result=-1;
                     }
                     if($scoreA==$scoreB){
                     	$result=0;
                     }
                     $endtime=0;
                     $state=1;
                      $starttime=$data->sheets[0]['cells'][$i][5];
                       $detail= $data->sheets[0]['cells'][$i][6];
                       $detail=iconv("utf-8","gbk",$detail);
                        $timeflag= $data->sheets[0]['cells'][$i][7];
                         $locaild= $data->sheets[0]['cells'][$i][8];
                         $locaild=iconv("utf-8","gbk",$locaild);
                         $id=$m->findchang($locaild);
                         	if(empty($id)){
                         		$m->addchang($locaild);
                         		  $id=$m->findchang($locaild);
                         	}
   $c= strtotime($starttime);
           
 	
	mysql_query("set default gbk");
	$sql="insert into matches values('','$idteamA','$idteamB','$nameteamA','$nameteamB','$scoreA','$scoreB','$c','$endtime','$result','$state','$detail','$timeflag','$id');";
	$res=mysql_query($sql);
	if(!$res){
		echo "比赛添加失败";
	}
                         }
        @unlink( "upload/" . $_FILES["foot"]["name"]);   
      }
    
   
      }
    }
  
else
  {
  	$alert=iconv("utf-8","gbk","只能上传2M大小的(2003-2005)excel文件");
  echo $alert;
  }
  class match{
 public  function findteam($nameteam){
  $conn=mysql_connect('localhost','root','123456')or die("连接失败".mysql_errno());
				
				
				mysql_select_db("stu",$conn)or die("连接数据库失败".mysql_errno());
			
				mysql_query("set names 'gbk'");
	$sql="select id from team where tname='{$nameteam}';";
	  $res=mysql_fetch_array(mysql_query($sql));
	if(empty($res)){
		$sql="insert into team values('','$nameteam','');";
	mysql_query($sql);{
		
	}
	}
	return $res['id'];
  }
  function findchang($locaild){
  		 $conn=mysql_connect('localhost','root','123456')or die("连接失败".mysql_errno());
				
				
				mysql_select_db("stu",$conn)or die("连接数据库失败".mysql_errno());
			
				mysql_query("set names 'gbk'");
	
$sql="select id from chang where name='$locaild'";
   $res=mysql_fetch_array(mysql_query($sql));
	if(!res){
		echo "查询失败".mysql_error();
	}
	return $res['id'];
  }
  function addchang($rs){
   $conn=mysql_connect('localhost','root','123456')or die("连接失败".mysql_errno());
				
				
				mysql_select_db("stu",$conn)or die("连接数据库失败".mysql_errno());
			
				mysql_query("set names 'gbk'");
$sql="insert into chang values('','$rs');";
	$res=mysql_query($sql);
	if(!$res){
		echo "场地添加失败";
	}	
  }
  }
 
?>

download.php

<?php
$name="data.xls";
 $path="download/$name";
 if(file_exists($path)){
    		header('Cache-control: max-age=31536000');
			header('Expires: '.gmdate('D, d M Y H:i:s', time() + 31536000).' GMT');
			header('Content-Encoding: none');
			header("Accept-Ranges:bytes"); 
 	header("Content-type:application/vnd.ms-excel");
    header( 'Content-Length: '.filesize($path));
	header("Content-Disposition:attachment;filename=".$name);
	readfile($path);
			exit;
 } else{
 	echo "文件不存在";
 }
?>


你可能感兴趣的:(php excel导入)