PHP csv导出数据

Posted 搬砖小伙子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP csv导出数据 相关的知识,希望对你有一定的参考价值。

 

 

 全部导出和时间导出

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();
复制代码

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

人生得意须尽欢,莫使金樽空对月.
 
 
 
 
-----------------------------------------------------------------------------自己项目实战------------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

注意:请求方法必须是get请求,不能用post请求

 

//用户提现审核导出
        public function check_daochu()
        {


                $req = Db::name(\'cash_out\')->alias(\'c\')
                  ->field(\'ca.card_name,ca.card_no,c.add_time,c.actual_money\')
                  ->join(\'xcf_user_wallet uw\', \'uw.user_id=c.user_id\', \'left\')
                  ->join(\'xcf_cash_account ca\', "ca.id=c.account_id", "left")
                  ->join(\'xcf_users u\', "u.user_id=c.user_id", "left")
                  ->where(\'c.role_id\', 1)
                  ->where(\'c.status\', 1)
                  ->order(\'c.add_time desc\')
                  ->select();
//                    ["card_name"] => string(6) "王毅"
//                    ["card_no"] => string(11) "15327953555"
//                    ["add_time"] => string(19) "2019-10-22 15:46:34"
//                    ["actual_money"] => string(4) "2.47"
                $csv_data = \'\';
                //标题
                $csv_data .= "姓名,支付宝账号,实际到账金额,提现时间\\r\\n";

                $row = array();
                foreach ($req as $k=>$val){
                    $row[\'store_name\'] = $val[\'card_name\'];
                    $row[\'bloc_name\'] = $val[\'card_no\'];
                    $row[\'dealer_name\'] = $val[\'actual_money\'];
                    $row[\'create_time\'] =$val[\'add_time\'];
                    $csv_data .= join(\',\', array_values($row))."\\r\\n";
                }

                $file_name = date(\'Y-m-d H-i-s\', time()).\' 用户提现.csv\';
                //导出数据
                $url = $this->csvExportWithBOM($file_name, $csv_data);
                exit();
                //$this->success(\'成功!\',\'\',$url);
               // return $url;
                //return json([\'ok\' => \'success\']);


        }

        /**@param $filename
         * 输出csv文件的文件名
         * @param $data
         * 要写入csv文件的内容,即经过csvGenerator处理过的字符串;
         */
        public function csvExportWithBOM($filename=\'export.csv\', $data)
        {
            $f=fopen("php://memory",\'w\');
            fwrite($f, chr(0xEF).chr(0xBB).chr(0xBF).$data);//加入BOM头
            fseek($f,0);
            header(\'Content-Type: application/csv\');
            // 告知浏览器想要输出的是一个文件
            header(\'Content-Disposition: attachment; filename="\'.$filename.\'";\');
            // make php send the generated csv lines to the browser
            fpassthru($f);

        }

 

 

以上是关于PHP csv导出数据 的主要内容,如果未能解决你的问题,请参考以下文章

PHP如何导出导入CSV文件?

PHP - 导出CSV函数重复每个条目的标题

求源码!PHP导出数据到csv文件

php 导入/导出 csv 文件

使用 PHP/MySQL 导出大型 CSV 数据的最佳方法是啥?

通过 PHP 导出为 CSV