PHP 导出EXCEL

格式:
 function createxcel($array){
        $strTable ='';
        $strTable .= '';
        $strTable .= 'id';
        $strTable .= '使用时间';
        $strTable .= '卡号';
        $strTable .= '绑定商家';
        $strTable .= '使用状态';
        $strTable .= '使用者';
        $strTable .= '是否中奖';
        $strTable .= '中奖等级';
        $strTable .= '';
        foreach ($array as $key=>$val){
            $strTable .= '';
            $strTable .= ' '.$key.'';
            $strTable .= ''. date("Y-m-d h:i", $val['addtime']).' ';
            $strTable .= ''.$val['numbers'].'';
            $strTable .= ''.$val['mch_name'].'';
            $val['is_use'] == 1 ?  $isuse = '是' : $isuse = '否' ;
            $strTable .= ''.$isuse.'';
            $strTable .= ''.$val['username'].'';
            $val['is_lucky'] == 1 ?  $islucky = '是' : $islucky = '否' ;
            $strTable .= ''.$islucky.'';
            $levelname = getlevelname($val['level']);
            $strTable .= ''.$levelname.'';
        }
        downloadExcel($strTable,'card');
        exit;
    }
    

使用函数:

/**
 * 导出excel
 * @param $strTable    表格内容
 * @param $filename 文件名
 */
function downloadExcel($strTable,$filename)
{
    header("Content-type: application/vnd.ms-excel");
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d').".xls");
    header('Expires:0');
    header('Pragma:public');
    echo ''.$strTable.'';
}

你可能感兴趣的:(程序)