dd

csv 文件操作

jerry thinkphp 2015年11月19日 收藏
php处理csv文件,csv可以用excel文件打开,操作方便.
<?php


$data = array (
        array (
                '张三',
                '男',
                '34岁',
                '北京' 
        ),
        array (
                '李四',
                '男',
                '38岁',
                '天津' 
        ),
        array (
                '王五',
                '男',
                '90岁',
                '湖北' 
        ),
        array (
                '赵六',
                '男',
                '12岁',
                '陕西' 
        ) 
);



/*
 * 功能描述:将数组数据写入cvs文件
 * 
 * 参数:
 * $data:需要写入的数组格式的数据
 * $filename:csv文件的名称,如果没有确定,默认为当前时间(格式为:YmdHis)
 * 
 * 
 * */
function write_csv($data = null, $filename = null) {
    if (! ($data || $filename)) {
        return false;
    }
    
    $fp = fopen ( $filename , 'w' );
    
    foreach ( $data as $fields ) {
        fputcsv ( $fp, $fields );
    }
    
    fclose ( $fp );
}


/*
 * 功能描述:不写入cvs文件,并且输出的到浏览器
 *
 * 参数:
 * $data:需要写入的数组格式的数据
 * $filename:csv文件的名称,如果没有确定,默认为当前时间(格式为:YmdHis)
 *
 *
 * */
function output_csv($data = null, $filename = null) {
    
    if (! ($data || $filename)) {
        return false;
    }
    
    header('Content-Type:application/force-download');
    header("content-Disposition:filename={$filename}");
    
    foreach ( $data as $fields ) {
        foreach ($fields as $value){
            echo $value.',';
        }
        echo "\r\n";
    }
    


    
}


/*
 * 功能描述:读取cvs文件
 *
 * 参数:
 * $filename:要读取的csv文件的名称
 *
 *
 * */
function read_csv($filename=null) {
    if(!$filename){
        return false;
    }

    $handle=fopen($filename,'r');
    if(!$handle){
        return false;
    }
    
    $row=1;
    while($data=fgetcsv($handle,1000,",")){
        $num=count($data);
        echo "<h1>  第 $row 行 ,共有 $num 个字段 <br /></h1>\n";
        $row ++;
        for ($c=0;$c<$num;$c++){
            echo $data[$c]."\t";
        }
    }
    

}

/*
 * 功能描述:读取cvs文件,输出到浏览器,采用file_get_contents处理
 *
 * 参数:
 * $filename:要读取的csv文件的名称
 *
 *
 * */
function read_output_file_get_contents($filename=null){

    if(!$filename){
        return false;
    }
    header('Content-Type:application/force-download');
    header("content-Disposition:filename={$filename}");
    
    echo file_get_contents($filename);

}

/*
 * 功能描述:读取cvs文件,输出到浏览器,采用fopen处理
 *
 * 参数:
 * $filename:要读取的csv文件的名称
 *
 *
 * */
function read_output_fopen($filename=null){

    if(!$filename){
        return false;
    }
    header('Content-Type:application/force-download');
    header("content-Disposition:filename={$filename}");
    
    
    $file_handle = fopen($filename, "r");
    while (!feof($file_handle)) {
        $line = fgets($file_handle);
        echo $line;
    }
    fclose($file_handle);

}

//write_csv($data,'test.csv');//将数据写入csv文件

//output_csv($data,'test.csv');//不写入csv文件,直接输出到浏览器下载

//read_csv('test.csv');//读取指定的csv文件

//read_output_file_get_contents('test.csv');//读取cvs文件,输出到浏览器,采用file_get_contents处理

//read_output_fopen('test.csv');//读取cvs文件,输出到浏览器,采用fopen处理
dd