导出CSV格式

/**
 * 导出数据格式为csv
 */
function export_to_csv($data = array(), $title = array(), $filename=null) {
    if(empty($filename)) {
        $filename = date('Ymd');
    }
    header("Content-type:text/csv"); 
    header("Accept-Ranges:bytes");
    header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename=".$filename.".xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    //导出xls 开始
    if (!empty($title)){
        foreach ($title as $k => $v) {
            $title[$k]=iconv("UTF-8", "GB2312",$v);
        }
        $title= implode("\t", $title);
        echo "$title\n";
    }
    if (!empty($data)){
        foreach($data as $key=>$val){
            foreach ($val as $ck => $cv) {
                $data[$key][$ck]='="'.iconv("UTF-8", "GB2312", $cv).'"';
            }
            $data[$key]=implode("\t", $data[$key]);
    
        }
        echo implode("\n",$data);
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券