php原生导出excel表格

2019-06-18  本文已影响0人  echomaple

public function excel(){

        $uarr = [1=>'用户',2 => '会员', 3 => '金牌', 4 => '经理'];

        $list = Db::name('table_update')->alias("up")

            ->join('user u', 'up.uid=u.id', 'LEFT')

            ->field("up.money,up.level,up.time2,up.time3,u.mername,u.mobile")

            ->where('up.time2','>','0')

            ->whereOr('up.time3','>','0')

            ->select();

        header("Content-type:application/vnd.ms-excel");

        header("Content-Disposition:attachment;filename=test_data.xls");

        echo  iconv("utf-8","gb2312//IGNORE",'姓名')."\t";

        echo  iconv("utf-8","gb2312//IGNORE",'手机号码')."\t";

        echo  iconv("utf-8","gb2312//IGNORE",'初始等级')."\t";

        echo  iconv("utf-8","gb2312//IGNORE",'累计收益')."\t";

        echo  iconv("utf-8","gb2312//IGNORE",'升级时间1')."\t";

        echo  iconv("utf-8","gb2312//IGNORE",'升级时间2)."\t";

        echo  "\n";

foreach ($list as $v){

    echo  iconv("utf-8","gb2312//IGNORE",$v['mername'])."\t";

    echo  $v['mobile']."\t";

    echo  iconv("utf-8","gb2312//IGNORE",$uarr[$v['level']])."\t";

    echo  $v['money']."\t";

    echo  ($v['time2']>0?date('Y-m-d H:i:s',$v['time2']):'');

    echo "\t";

    echo  ($v['time3']>0?date('Y-m-d H:i:s',$v['time3']):'')."\t";

    echo  "\n";

}

    }

浏览器直接访问该方法路径即可导出excel!

上一篇下一篇

猜你喜欢

热点阅读