2012年9月27日 星期四

php excel

EXCEL讀檔 寫入MYSQL

include_once("../XXX/phpexcel/PHPExcel/IOFactory.php");
include_once('../XXX/phpexcel/PHPExcel/Reader/Excel5.php');
 if($_FILES['employeeData']['name']=="" )"ERROR!!no FILE!!";

 $reader = new PHPExcel_Reader_Excel5;
 $PHPExcel=$reader->load($_FILES['exployxeData']['tmp_name']);
 $sheet=$PHPExcel->getSheet(0);
 $highestRow=$sheet->getHighestRow();

for($row=1;$row<=$highestRow;$row++){
 //echo $i."$$";
 $str='';
  for($column=0;$column<=6;$column++){
   $aa=$sheet->getCellByColumnAndRow($column,$row)->getvalue();
    $temp[$column]=$aa;
    if($column=='0'){
     $str.="'".$aa."',";//id
     $str.="'".$aa."',";//pwd
    }elseif($column=='6'){
     if($temp[4]==$temp[0]){
      $str.="'1',";   
     }elseif($temp[6]=='1'){
      $str.="'2',";
     }else{
      $str.="'0',";
     }
     
    
    }elseif($column=='5'){
     continue;
    }else{
     if($aa!=''){
      $str.="'".$aa."',";
     }
    }
   
  }
   
   $str=substr($str,0,strlen($str)-1);
   
   
  if($row>1){
   $sql="insert into Base  (`ID`,`Pwd`,`Name`,`Name`,`Email`,`lID`,`role`)values($str)";
   mysql_query($sql);
   
   if(! mysql_affected_rows()>0){
    echo "error:name:{$temp[1]}";
    exit;
   }
  }
 }

寫入excel
include_once("../class/phpexcel/PHPExcel/IOFactory.php");

$path="../download/";
 $objRD=PHPExcel_IOFactory::createReader("Excel5");
 
 $objExcel=$objRD->load($path."temp.xls");
 //$CobjExcel=new PHPExcel();

 //$objExcel=$CobjExcel->load($path."temp.xls");
 
 $objExcel->setActiveSheetIndex(0);
 $sheet=$objExcel->getActiveSheet();

 for($i=1;$i<$num;$i++){
  //if($data[$i]['enable']==0) continue; #因為sql裡面排除不了enable為0的,所以做此處理
         $sheet->setCellValueByColumnAndRow(0,$colum,$searchContent[$i]['userName']);
  $sheet->setCellValueByColumnAndRow(1,$colum,$searchContent[$i]['ID']);           
  $sheet->setCellValueByColumnAndRow(2,$colum,$searchContent[$i]['shifName']);
$colum++;
  }
  
  
  $objExcel=PHPExcel_IOFactory::createWriter($objExcel,'Excel5');

  $filePath=$path."labor_hour.xls";
  $objExcel->save($filePath);
  
  
  dl_file($filePath);  

沒有留言: