• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

PHPcsv导出数据

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

 全部导出和时间导出

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();

 

/**
 * @param $file_name //请求参数 文件名
 * @param $content //请求参数 根据内容
 * @return  url
 * 保存文件导出csv
 */
function save_csv($file_name,$content){
    $content = mb_convert_encoding($content, "cp936", "UTF-8");
    //这里的CSV即你项目的根目录下新建一个CSV文件夹
    //我们给每个用户动态的创建一个文件夹
    $file_path = 'CSV/'.date('Ymd', time());
    //判断该用户文件夹是否已经有这个文件夹
    if(!file_exists("./upload/".$file_path)) {
        mkdir("./upload/".$file_path, 0777, true);
    }
    $path = ('./upload/'.$file_path.'/'.$file_name);
    $fp = fopen($path,'a');
    // 写入并关闭资源
    fwrite($fp, $content);
    fclose($fp);
    $path = cmf_get_root() . '/upload/' .$file_path.'/'.$file_name;
    return $path;
}
/**
 * 获取网站根目录
 * @return string 网站根目录
 */
function cmf_get_root()
{
    $request = Request::instance();
    $root    = $request->root();
    $root    = str_replace('/index.php', '', $root);
    if (defined('APP_NAMESPACE') && APP_NAMESPACE == 'api') {
        $root = preg_replace('/\/api$/', '', $root);
        $root = rtrim($root, '/');
    }

    return $root;
}

 

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP实现正态分布的累积概率函数发布时间:2022-07-10
下一篇:
php://input 和 $HTTP_ROW_POST_DATE发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap