«

记录一个php导出csv方法

时间:2024-4-30 11:03     作者:杨佳乐     分类: PHP


 public function export_excel(){

       //查询列表传入即可

        $html = $this->wxapp_house_export_parse_zan($list);
        header("Content-type:text/csv");
        header("Content-Disposition:attachment; filename=点赞信息.csv");
        echo $html;
        exit();

    }

    //改善后
        function export_jslog($list) {
            if (empty($list)) {
                return false;
            }
            $header = array(
                'id'=>'id',
                'openid' => 'openid',
                'nickname' => '用户名称',
                'mobile'=>'手机号',
                'province'=>'省',
                'city'=>'市',
                'area'=>'区',
                'mj'=>'面积',
                'shi'=>'室',
                'ting'=>'厅',
                'chu'=>'厨',
                'wei'=>'卫',
                'addtime'=>'计算时间'
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";

            foreach ($list as $row) {

                $fans=$this->get_fansinfo($row['openid']);
                $row['nickname']=$fans['nickname'];
                $row['addtime']=date('Y-m-d H:i:s',$row['addtime']);
                foreach ($keys as $key) {
                    $data[] = $row[$key];
                }
                $user[] = implode("\t ,", $data) . "\t ,";
                unset($data);
            }

            $html .= implode("\n", $user) . "\n";

            return $html;
        }

    function wxapp_house_export_parse_zan($cards) {
        if (empty($cards)) {
            return false;
        }
        $header = array(
            'openid' => 'openid',
            'name' => '用户名称','phone'=>'手机号','dw'=>'单位','zw'=>'职务','address'=>'地址'
        );
        $keys = array_keys($header);
        $html = "\xEF\xBB\xBF";
        foreach ($header as $li) {
            $html .= $li . "\t ,";
        }
        $html .= "\n";
        $count = count($cards);
        $pagesize = ceil($count / 5000);
        for ($j = 1; $j <= $pagesize; $j++) {
            $list = array_slice($cards, ($j - 1) * 5000, 5000);
            if (!empty($list)) {
                $size = ceil(count($list) / 500);
                for ($i = 0; $i < $size; $i++) {
                    $buffer = array_slice($list, $i * 500, 500);
                    $user = array();
                    foreach ($buffer as $row) {
                        $area = pdo_fetchcolumn("SELECT name FROM " . tablename('amouse_wxapp_category') . " WHERE id=:id  limit 1", array(":id" => $row['categoryId']));
                        $row['categoryId'] = $area;
                        $row['createtime'] = date('Y-m-d H:i:s', $row['createtime']);
                        foreach ($keys as $key) {
                            $data[] = $row[$key];
                        }
                        $user[] = implode("\t ,", $data) . "\t ,";
                        unset($data);
                    }
                    $html .= implode("\n", $user) . "\n";
                }
            }
        }
        return $html;
    }