csv数据的导出,平时使用Thinkphp处理数据,不过代码和以下的代码逻辑类似!不依赖任何第三方的类库
//数据处理
function exportData(){
$result = mysql_query("select * from student");
$str = "姓名,性别,年龄\n";
$str = iconv('utf-8','gb2312',$str);
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);
}
//输出
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;
}