PHP生成CSV文件的方法

PHP生成CSV文件的方法,使用了两个class,请看下文:
生成CSV文件,在excel里打开生成的文件的时候分隔符选择","逗号!

<?
/***********************************************************

Author:www.web745.com
    Description:                PHP生成csv文件
    Version:                        1.0
        Date:                                2004-8-18
************************************************************/
require_once("yyFile.class.php");

class csv extends yyFile{
        var $mSpace                        =',';                        //分隔字符
        

        var $mHead;                                                        //标题
        var $mBody;                                                        //主体

        /*
        *方法   writeFile
        *功能   写文件
        *参数        无
        *返回   成功:返回int                失败:0
    */
        function writeFile(){
                return fwrite($this->mFp,$this->mHead.$this->mBody);
        }//end

        /*
        *方法    setHead
        *功能    设定一个头
        *参数                array $head                头的内容
        *返回    无
    */
        function setHead($head=array()){
                if(is_array($head))
                        $this->mHead=implode($this->mSpace,$head)."\n";
        }

        /*
        *方法   setBody
        *功能   设定一个主体
        *参数        array        $body                主要内容
                                array        $order                输出顺序
        *返回   无
    */
        function setBody($body=array(),$order=array()){
                $this->mBody='';
                if(count($order)==0)//$order不存在
                        $order=array_keys($body);

                $len=count($body[$order[0]]);
                if($len==0){
                        echo "要显示的数组的格式错误,或排序参数错误";
                        return 0;
                }
                for($i=0;$i<$len;$i++){//循环
                        foreach($order as $v){//根据order的顺序排列
                                if(array_key_exists($v,$body)){//order的内容是body的下标
                                        $this->mBody.=$body[$v][$i].$this->mSpace;
                                }
                        }
                        $this->mBody.="\n";
                }
               
                //echo $this->mBody;
        }//end

        /*
        *方法   setSpace
        *功能   设定间隔符号
        *参数        string $space                分隔符
        *返回   无
    */
        function setSpace($space=','){
                $this->mSpace=$space;
        }//end


}//end csv

$fp=new csv();
//$fp->setFileName("yy.csv");
//$fp->setPath("../../");
$fp->openFile();
$fp->setHead(array('a','b','c'));
$fp->setBody(array('a'=>array('1','2','3'),'b'=>array('b1','b2','b3'),'c'=>array('c1')));
$fp->writeFile();
$fp->closeFile();
echo $fp->getFileName();

?>
[/php]

yyFile.class.php
[php]
<?
/***********************************************************
    Author: www.web745.com    本class负责文件操作
************************************************************/
class yyFile{
        var $mFileName;                                                                        //文件名
        var $mExtendName                ="txt";                                //文件扩展名
        var $mPath                                ="./";                                        //路径

        var $mFp;                                                                                //打开文件句柄
        
        /*
        *方法    openFile
        *功能    打开一个文件
        *参数                $mode                打开文件的格式
        *返回    成功:返回句柄                失败:0
    */
        function openFile($mode='w'){
                if(empty($this->mFileName))
                        $this->setTimeFileName();
                if(empty($this->mExtendName))
                        $this->setExtendName;
                $fp=fopen($this->mPath."/".$this->mFileName.".".$this->mExtendName,$mode);
                if($fp)
                        $this->mFp=$fp;
                else
                        return 0;
        }//end openFile

        /*
        *方法   closeFile
        *功能   关闭一个文件
        *参数        无
        *返回   bool
    */
        function closeFile(){
                return fclose($this->mFp);
        }//end
        
        /*
        *方法   getFileName
        *功能   得到路径文件名
        *参数        无
        *返回   string
    */
        function getFileName($name=''){
                if(substr($this->mPath,-1)=="/")
                        $this->mPath=substr($this->mPath,0,-1);
                return StripSlashes($this->mPath."/".$this->mFileName.".".$this->mExtendName);
        }//end

        //取得文件扩展名
        function getExtendName($file_name) {
                $pt=strrpos($file_name, ".");
                if ($pt) $this->mExtendName=substr($file_name, $pt+1, strlen($file_name) - $pt);               
        }

        /*
        *方法    setFileName
        *功能    设定一个文件名
        *参数                $type                文件名的格式时间格式date()的参数
        *返回    无
    */
        function setFileName($name=''){
                $last=strrpos($name,'.');
                if(!empty($name))
                        if(is_int($last)){//有扩展名
                                $this->mFileName=substr($name,0,$last);
                                $this->mExtendName=substr($name, $last+1, strlen($name) - $last);
                        }
                        else{
                                $this->mFileName=$name;
                        }
                else
                        $this->mFileName=$this->getFileName();
        }//end

        /*
        *方法    setTimeFileName
        *功能    生成一个文件名
        *参数                $type                文件名的格式时间格式date()的参数
        *返回    无
    */
        function setTimeFileName($type='Ymd'){
                if(!empty($type))
                        $this->mFileName=date($type);
                else
                        $this->mFileName=time();
        }



        /*
        *方法    setExtendName
        *功能    生成一个文件扩展名
        *参数                $e                文件扩展名
        *返回    无
    */
        function setExtendName($e="txt"){
                $this->mExtendName=$e;
        }//end setExtendName

        /*
        *方法    setPath
        *功能    建立目录
        *参数    $path:目录
                $mode:格式
        *返回    str
    */
        
    function setPath($path,$mode = 0700) {
        //$path=str_replace("/","\",$path);
        $dirs = explode("\\",realpath($path));
        $path = $dirs[0];
        for($i = 1;$i < count($dirs);$i++) {
            $path .= "/".$dirs[$i];
            if(!is_dir($path))
                mkdir($path,$mode);
        }
               
        if(is_dir($path)){
                        $this->mPath=$path;
        }
        else{
            return 0;
        }
               
    }
}

?>


补充fp->setBody(array('a'=>array('1','2','3'),'b'=>array('b1','b2','b3'),'c'=>array('c1')));
这个方法的第一个参数可以用我写的db class 里的getRes方法得到的值直接传进去就可以!

第二个参数代表字段的输出顺序!可以不写。不写就是第一个参数的第一个下标的顺序!