码迷,mamicode.com
首页 > Web开发 > 详细

PHP csv导出数据 (二)

时间:2019-11-13 16:31:56      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:foreach   include   ajax   laydate   creat   日期范围   ack   类型   bre   

 

技术图片

 

 全部导出和时间导出

html代码,全程并不需要引用什么插件

技术图片
<include file="public@header"/>
<link href="__TMPL__/public/assets/css/style.css" rel="stylesheet">
</head>
<body>
<div class="margin-top-20 body_top">
    <div class="form-inline">
        <input type="submit" name="all" class="btn btn-info quanbu" value="全部导出"/>

        <input style="margin-left: 15px" type="text" name="data" placeholder="日期范围" class="form-control month" id="test6" autocomplete="off">
        <input type="submit" class="btn btn-info shijian" value="导出"/>
    </div>
</div>

</body>
<script src="__STATIC__/js/admin.js"></script>
<script src="__STATIC__/js/laydate/laydate.js"></script>
<script>
    //日期范围
    laydate.render({
        elem: ‘#test6‘
        ,range: true
    });

    $(‘.quanbu‘).on(‘click‘,function () {
        $.ajax({
            url: "/systems/Sum/quanbu",
            type: "post",
            dataType: "json",
            // data:{‘data‘:nian},
            success: function (back) {
                if (back.code == 1) {
                    document.location.href = (back.data);
                } else {
                    layer.msg("导出csv出错!");
                }
            },
            error: function (e) {
                console.log(e)
            }
        });
    });

    $(‘.shijian‘).on(‘click‘,function () {
        var data=$(‘.month‘).val();

        $.ajax({
            url: "/systems/Sum/shijian",
            type: "post",
            dataType: "json",
            data:{‘data‘:data},
            success: function (back) {
                if (back.code == 1) {
                    document.location.href = (back.data);
                } else {
                    layer.msg("导出csv出错!");
                }
            },
            error: function (e) {
                console.log(e)
            }
        });
    })
</script>
技术图片

 

php代码

技术图片
 public function quanbu(){
        //全部导出
        $req=Db::name(‘cy_sum‘)
            ->alias(‘a‘)
            ->field(‘s.store_name,b.company_name as bloc_name,d.company_name as dealer_name,a.type,a.garment,a.garment_function,a.clothing,a.clothing_function,a.functional_finishing,a.create_time‘)
            ->join(‘store s‘,‘a.sid=s.id‘)
            ->join(‘bloc b‘,‘a.bid=b.id‘)
            ->join(‘dealer d‘,‘a.did=d.id‘)
            ->select()
            ->toArray();

        foreach ($req as $k=>$v){
            switch ($req[$k][‘type‘]) {
                case 1:
                    $req[$k][‘type‘] = ‘定制‘;
                    break;
                case 2:
                    $req[$k][‘type‘] = ‘定制团单‘;
                    break;
                case 3:
                    $req[$k][‘type‘] = ‘样衣‘;
                    break;
                case 4:
                    $req[$k][‘type‘] = ‘大货‘;
                    break;
                case 5:
                    $req[$k][‘type‘] = ‘面料试样‘;
                    break;
                case 6:
                    $req[$k][‘type‘] = ‘售后订单‘;
                    break;
            }
        }

        $csv_data = ‘‘;
        //标题
        $csv_data .= "门店,集团,经销商,订单类型,成衣数量,成衣+功能数量,制衣数量,制衣+功能数量,后整理加工,日期\r\n";

        $row = array();
        foreach ($req as $k=>$val){
            $row[‘store_name‘] = $val[‘store_name‘];
            $row[‘bloc_name‘] = $val[‘bloc_name‘];
            $row[‘dealer_name‘] = $val[‘dealer_name‘];
            $row[‘type‘] = $val[‘type‘];
            $row[‘garment‘] = $val[‘garment‘];
            $row[‘garment_function‘] = $val[‘garment_function‘];
            $row[‘clothing‘] = $val[‘clothing‘];
            $row[‘clothing_function‘] = $val[‘clothing_function‘];
            $row[‘functional_finishing‘] = $val[‘functional_finishing‘];
            $row[‘create_time‘] = date(‘Y-m-d‘,$val[‘create_time‘]);
            $csv_data .= join(‘,‘, array_values($row))."\r\n";
        }

        $file_name = date(‘YmdHis‘, time()).‘detail.csv‘;
        //导出数据
        $url = save_csv($file_name, $csv_data);

        $this->success(‘成功!‘,‘‘,$url);
        exit();
    }

    public function shijian(){

        $list=$this->request->param();
        //时间导出
        $data=explode("- ",$list[‘data‘]);

        $data[0]=strtotime($data[0]);
        $data[1]=strtotime($data[1]);

        $req=Db::name(‘cy_sum‘)
            ->alias(‘a‘)
            ->field(‘s.store_name,b.company_name as bloc_name,d.company_name as dealer_name,a.type,a.garment,a.garment_function,a.clothing,a.clothing_function,a.functional_finishing,a.create_time‘)
            ->join(‘store s‘,‘a.sid=s.id‘)
            ->join(‘bloc b‘,‘a.bid=b.id‘)
            ->join(‘dealer d‘,‘a.did=d.id‘)
            ->whereTime(‘a.create_time‘, ‘between‘, [$data[0], $data[1]])
            ->select()
            ->toArray();

        if (empty($req)){
            $this->error(‘该时间段无数据 !‘);
        }

        foreach ($req as $k=>$v){
            switch ($req[$k][‘type‘]) {
                case 1:
                    $req[$k][‘type‘] = ‘定制‘;
                    break;
                case 2:
                    $req[$k][‘type‘] = ‘定制团单‘;
                    break;
                case 3:
                    $req[$k][‘type‘] = ‘样衣‘;
                    break;
                case 4:
                    $req[$k][‘type‘] = ‘大货‘;
                    break;
                case 5:
                    $req[$k][‘type‘] = ‘面料试样‘;
                    break;
                case 6:
                    $req[$k][‘type‘] = ‘售后订单‘;
                    break;
            }

        }

        $csv_data = ‘‘;
        //标题
        $csv_data .= "门店,集团,经销商,订单类型,成衣数量,成衣+功能数量,制衣数量,制衣+功能数量,后整理加工,日期\r\n";

        $row = array();
        foreach ($req as $k=>$val){
            $row[‘store_name‘] = $val[‘store_name‘];
            $row[‘bloc_name‘] = $val[‘bloc_name‘];
            $row[‘dealer_name‘] = $val[‘dealer_name‘];
            $row[‘type‘] = $val[‘type‘];
            $row[‘garment‘] = $val[‘garment‘];
            $row[‘garment_function‘] = $val[‘garment_function‘];
            $row[‘clothing‘] = $val[‘clothing‘];
            $row[‘clothing_function‘] = $val[‘clothing_function‘];
            $row[‘functional_finishing‘] = $val[‘functional_finishing‘];
            $row[‘create_time‘] = date(‘Y-m-d‘,$val[‘create_time‘]);
            $csv_data .= join(‘,‘, array_values($row))."\r\n";
        }

        $file_name = date(‘YmdHis‘, time()).‘detail.csv‘;
        //导出数据
        $url = save_csv($file_name, $csv_data);

        $this->success(‘成功!‘,‘‘,$url);
        exit();
技术图片

无论导出多大的数据,都不会出现乱码

PHP csv导出数据 (二)

标签:foreach   include   ajax   laydate   creat   日期范围   ack   类型   bre   

原文地址:https://www.cnblogs.com/yehuisir/p/11849929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!