导出成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;
}