cvs导出模板(防止乱码)

自定义cvs简易导出(防止乱码)

$firstLine = array(
	 'user_name' =>'会员',
	 'user_money' =>'投资金',
	 'gcoin_account' =>'Love',
	 'token_account' =>'LIFF',
	 'ingot_account' =>'LL',
	 'change_desc' =>'说明',
	 'change_time' =>'日期',
);
downPcSearch($info,$firstLine,'交易记录'.date('YmdHis'));die;

//data 表数据,firstLine 首行数据,文件名fileName 
function downPcSearch($data,$firstLine = array(),$fileName = null)
{
    if (count($data) > 0) {
	    if (!$fileName) {
	        $fileName = date("YmdHis");//文件名不存在则取当时时间戳
	    }
        set_time_limit(0);
        ini_set('memory_limit', '2048M');
        ini_set('max_execution_time',0);
        ob_end_clean();
        ob_start();
        header("Content-Type: text/csv");
        header("Content-Disposition:filename=".$fileName.".csv");
        $output = fopen('php://output','w');
        fwrite($output, chr(0xEF).chr(0xBB).chr(0xBF));//转码 防止乱码(比如微信昵称(乱七八糟的))
        fputcsv($output, array_values($firstLine));
        foreach ($data as $_data) {
            $_output = array();
            foreach ($firstLine as $key => $val) {
                $_output[$key] = $_data[$key];
            }
            fputcsv($output, array_values($_output));
        }
        ob_flush();
        flush();
        ob_end_clean();
        exit;
    } else {
        echo "<script>alert('数据为空');history.go(-1)</script>";
        die;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_16618179/article/details/77945412
cvs