>  기사  >  백엔드 개발  >  php 导入数据到excel_PHP教程

php 导入数据到excel_PHP教程

WBOY
WBOY원래의
2016-07-20 11:06:54765검색

class CsvFieldDump  {

 var $headers;
 var $fieldnum;
 function CsvFieldDump($infile){
  if(empty($infile)){
   die("You must specify a csv file to readn");
  }
  if(!file_exists($infile)){
   die("$infile doesn't exist!n");
  }
  $this->infile  = $infile;
  $this->fieldnum = $field;
  $this->createFile=0;
  return(true);
 }

 var $headers=array();
 function dump_headers($dumptoscreen=1){
  $this->fd=fopen($this->infile, "r") or die("Couldn't open csv file $file");
  $count=0;
  while($data = fgetcsv($this->fd, 1000, ",") ){
   if(!$count){
    if($dumptoscreen){
     print_r($data);
    }
    $this->headers=$data;
   }
   $count=1; 
  }
  return($this->headers);
 }

 function dump_column($num){


  $this->fieldnum = $num;
  $this->fd=fopen($this->infile, "r") or die("Couldn't open csv file $file");
  $count=0;
  while($data = fgetcsv($this->fd, 1000, ",")){
   $array=array();
   if(is_array($this->fieldnum)){
    foreach($this->fieldnum as $num){
     $array[]=$data[$num];
    }
   }else{
    $array = array($data[$this->fieldnum]);
   }
   $this->write_line($array);
  }
 }


 var $colname;
 var $headers_flip;
 function dump_column_by_name($name){
  if(!isset($name)){
   die("No field name specified");
  }
  $this->colname=$name;
  $this->dump_headers(0);
  $this->headers_flip = array_flip($this->headers);
  if(is_array($this->colname)){
   foreach($this->colname as $n){
    $array[] = $this->headers_flip[$n];
   }
  }else{
   $array[] = $this->headers_flip[$this->colname];
  }

  $this->dump_column($array);
 }

 function write_line($data){
  $string="";
  $num=count($data);
  for($x=0;$x   $string.=$data[$x];
   if($x    $string.=",";
   }
  }
  echo $string."n";
 }

}

 

/////////////////////// make it think its a c program :)
if(isset($argv[1])){

 $filename = $argv[count($argv)-1];

 if($argv[1] == "-h"){
  render_help();
  die();
 }else{
  $b=new CsvFieldDump($filename);
 }

 $x=0;
 foreach($argv as $opt){
  switch($opt) {
   case "df":
   case "-df" :
    $number = $argv[$x+1];
    if(strstr($number, ",")){
     $numbers = explode(",",$number);
    }else{
     $numbers = $number;
    }
    $b->dump_column($numbers);
   break;

   case "-dn":
    $number = $argv[$x+1];
    if(strstr($number, ",")){
     $names = explode(",",$number);
    }else{
     $names = $number;
    }
    $b->dump_column_by_name($names);
   break;

   case "--help":
   case "-h" :
    render_help(); 
   break;

   case "-dh" :
    $b->dump_headers();
   break;
  }
  $x++;
 }

}
function render_help(){
echo "CsvFieldDump version 0.0.0.1 by mark fate_amendable_to_change@yahoo.com
-df     = dumps field(s) number n - also (accepts comma delimited list for multiple field numbers eg: -df 1,2,3)
-dn     = dumps field(s) named N (accepts comma delimited list for multiple names eg: -dn Code,Desc)
-dh        = dumps csv headers
";
}


?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445005.htmlTechArticleclass CsvFieldDump { var $headers; var $fieldnum; function CsvFieldDump($infile){ if(empty($infile)){ die(You must specify a csv file to readn); } if(!file_exists($infile)){ die($i...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.