php 导出成csv格式

导出成csv格式。并会判断是MAC系统的话导出MAC格式。否则导出成win和linux


public function export_excel() {

        ini_set('memory_limit', '256M'); /* 脚本最大占用内存大小 */
        set_time_limit(0);
        if ($this->page_params['no_permission']) {
            return;
        }


        $start_time = $this->input->post('export_start_time');
        $end_time = $this->input->post('export_end_time');
        $medium_type = $this->input->post('export_medium_type');
        $advertisement = $this->input->post('export_advertisement');
        $product = $this->input->post('export_product');
        $channel_id = $this->input->post('export_channel_id');


        $fields = $this->input->post('field');


        $search_condition = array(
            'start_time' => $start_time,
            'end_time' => $end_time,
            'medium_type' => $medium_type,
            'advertisement' => $advertisement,
            'product' => $product,
            'channel_id' => $channel_id
        );
        $this->page_params['search_condition'] = $search_condition;


        $data = $this->gf_channel_day_model->export_excel($start_time, $end_time, $medium_type, $advertisement, $product, $channel_id);


        if (!$data) {
            $this->page_params['tab'] = 'tab1';
            $this->view_list($start_time, $end_time, $medium_type, $advertisement, $product, $channel_id);
            return;
        }


        $titles = array(
            's_date' => '日期',
            'advertisement' => 'xx主',
            'medium_type' => '渠道',
            'product' => '产品',
            'channel_id' => 'xx名称',
            'exposure_num' => 'xx量',
            'click_num' => 'xx量',
            'click_rate' => 'xx率',
            'bid_num' => 'xx(元)',
            'order_num' => 'xx数',
            'total_num' => 'xx总数',
            'ip_num' => ' xx去重',
            'cpm' => 'CPM',
            'click_money_average' => 'xx均价',
            'cpa' => 'CPA',
            'money_num' => 'xx(元)'
        );


        $csv_str = '';
        $OS = $this->myutils->getOS();
        if ($OS == 'Macintosh') {
            $delim = ";";
        } else {
            $delim = ",";
        }
        $newline = "\n";
        $enclosure = '"';


        foreach ($titles as $name) {
            $csv_str .= $enclosure . str_replace($enclosure, $enclosure . $enclosure, $name) . $enclosure . $delim;
        }


        $csv_str = rtrim($csv_str);
        $csv_str .= $newline;


        // Next blast through the result array and build out the rows
        foreach ($data as $row) {
            foreach ($titles as $key => $value) {
                $item = '';
                switch ($key) {
                    case 'medium_type':
                        if ($row['medium_type']) {
                            $item = $this->medium_type[$row['medium_type']];
                        }
                        break;
                    case 'advertisement':
                        if ($row['ad_owner']) {
                            $item = $this->advertisement[$row['ad_owner']];
                        }
                        break;
                    case 'product':
                        if ($row['policy_type']) {
                            $item = $this->policy_type[$row['policy_type']];
                        }
                        break;
                    case 'cpm':
                        $item = (int) $row['exposure_num'] == 0 || empty($row['money_num']) ? '' : round($row['money_num'] / (int) $row['exposure_num'] * 1000, 2);
                        break;
                    case 'cpa':
                        $item = (int) $row['order_num'] == 0 || empty($row['money_num']) ? '' : round($row['money_num'] / (int) $row['order_num'], 2);
                        break;
                    default :
                        $item = $row[$key];
                        break;
                }


                $csv_str .= $enclosure . str_replace($enclosure, $enclosure . $enclosure, $item) . $enclosure . $delim;
            }
            $csv_str = rtrim($csv_str);
            $csv_str .= $newline;
        }


        $fileType = mb_detect_encoding($csv_str, array('UTF-8', 'GBK', 'LATIN1', 'BIG5'));
        if ($fileType != 'GBK') {
            $csv_str = mb_convert_encoding($csv_str, 'gbk', $fileType);
        }


        $outputFileName = 'xxxxx.csv';
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=" . $outputFileName);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $csv_str;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值