php导出csv

文章描述:

php导出csv文件

$str = "姓名,年龄\n";
$str = iconv('utf-8','gb2312',$str);
foreach($items as $key => $row){
    $name = iconv('utf-8','gb2312',$row['name']);
    $age= $row['age'];
    $str .= $name.",".$age."\n";
}

$filename = "学生信息".date('Ymd').'.csv';
export_csv($filename,$str);
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;
}

 

发布时间:2021/07/02

发表评论