<?php $sql = "select * from members_sqzj order by id asc"; $result = $db->fetch_All($sql);//查询数据 $str = "name,tel,company,shangquan\n"; $str = iconv(‘utf-8‘,‘gb2312‘,$str); foreach($result as $k=>$v){ $name = iconv(‘utf-8‘,‘gb2312‘,$v[‘name‘]); //中文转码 $tel = iconv(‘utf-8‘,‘gb2312‘,$v[‘tel‘]); $company = iconv(‘utf-8‘,‘gb2312‘,$v[‘company‘]); $shangquan = str_replace(‘,‘,‘|‘,$v[‘shangquan‘]);//不能出现“,” $shangquan = iconv(‘utf-8‘,‘gb2312‘,$shangquan); $str .= $name.",".$tel.",".$company.",".$shangquan."\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; } ?>
代码简单,header不好记。
时间: 2024-10-28 22:50:31