码迷,mamicode.com
首页 > Web开发 > 详细

php 导出csv文件

时间:2016-04-28 16:54:33      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

<?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不好记。

php 导出csv文件

标签:

原文地址:http://www.cnblogs.com/hyh123/p/5443170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!