php生成csv文件的两个类

来源:互联网 发布:鲁班软件和广联达 编辑:程序博客网 时间:2024/05/22 01:59
<?php
 require_once 'fileOperation.class.php';
 class csvHelper extends fileOperation{
     var $mSpace = ',';
     var $mHead;
     var $mBody;
     function addHeader($head=array()){
         if (is_array($head)){
             $this->mHead=implode(',',$head)."/r/n";
            
         }
     }
     function addBodyData($body=array()){
         if(is_array($body)){
             for($i=0;$i<count($body);$i++){
                 $childBody=$body[$i];
                 for($j=0;$j<count($childBody);$j++){
                     $this->mBody.=$childBody[$j].$this->mSpace;
                 }
                 $this->mBody.="/r/n";
             }
         }
        
     }
     function _construct(){
        
     }
     function writeCSVDate(){
         fwrite($this->mFp,$this->mHead.mb_convert_encoding($this->mBody,'sjis','sjis'));
     }
     function setSpace($type=','){
         $this->mSpace=$type;
     }
 }
 
?>

class : fileOperation
<?php
  class fileOperation {
    var $fileName;
    var $extendName='csv';
    var $mPath='./';
    var $mFp;
    function fileOperation() {
       
    }
    function openFile($mode='w'){
        if(empty($this->fileName)){
            $this->setTimeFileName();
        }
        if (empty($this->extendName)){
            $this->setExtendName();
        }
       
        $fp=fopen($this->mPath.'/'.$this->fileName.'.'.$this->extendName,$mode);
        if($fp){
            $this->mFp=$fp;
        }else{
            return 0;
        }
    }
    function closeFile(){
        return fclose($this->mFp);
    }
    function setTimeFileName($type='Ymd'){
        if(!empty($type)){
            $this->fileName=$type;
        }else{
            $this->fileName=time();
        }
    }
    function setExtendName($extend='txt'){
        if(!empty($extend)){
            $this->extendName=$extend;
        }else{
            $this->extendName='.csv';
        }
    }
    function setPath($path='./'){
        $this->mPath=$path;
    }
}


?>
原创粉丝点击