php导出csv

2022-01-13 10:54:59 浏览数 (1)

代码语言:javascript复制
function export_csv($filename,$data) { 
    header("Content-type:text/csv"); 
    header("Content-Disposition:attachment;filename=".$filename); 
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
    header('Expires:0'); 
    header('Pragma:public'); 
    echo $data;  
}
 while($row=mysql_fetch_array($result)){ 
        $name = iconv('utf-8','gb2312',$row['name']); //中文转码 
        $sex = iconv('utf-8','gb2312',$row['sex']); 
        $str .= $name.",".$sex.",".$row['age']."n"; //用引文逗号分开  
} 
$filename = date('Ymd').'.csv'; //设置文件名 
export_csv($filename,$str); //导出

0 人点赞