$filename = 'csv_'. time();
$title="标题1,标题2,标题N";
$this->export($filename, $title);
$this->csv($csv_res);
================ 下面是调用方法例子 =============================================================/**
* 导出csv文件
* @param 文件名 $filename
* @param 数据 $data
* @param 从数据中取指定字段 $fileds
* @param 字段名 多个用逗号分割 $title
* @param 数据量过大标记 $tooMuch*/
public function export($filename, $title,$tooMuch=0){
$filename= @iconv('UTF-8','GBK',$filename.'.csv');
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo @iconv('UTF-8', 'GBK//IGNORE', $title)."\r\n";
}/**
* [csv 导出数据]
* @param [type] $data [数据]
* @return [type] [description]*/
privatefunction csv($data)
{
$regex= "/[ '.,:;*?~`!@#$%^&+_=<>{}]|\]|\[|\/|\\|\"|\|/";if (!empty($data)) {foreach ($data as $key =>$value) {
$value['shop_name']=isset($value['shop_name'])?FuncHelper::escapeEmoji($value['shop_name']):'-';
$address=isset($value['address'])?$value['address']:'-';//门店地址
$shop_name=preg_replace($regex,"",$value['shop_name']);
$board_total= isset($value['board_total']) ? $value['board_total']:"0";
$first_login_time= isset($value['first_login_time']) ? date('Y-m-d H:i:s',$value['first_login_time']):"-";
$goods_count= isset($value['goods_count']) ? $value['goods_count']:"0";
$pay_sum= isset($value['pay_sum']) ? $value['pay_sum']:"0";
echo'"'.@mb_convert_encoding($value['id'], 'GBK', 'UTF-8').'",';
echo'"'.@mb_convert_encoding($value['uid'], 'GBK', 'UTF-8').'",';
echo'"'.@mb_convert_encoding($shop_name, 'GBK', 'UTF-8').'",';
echo'"'.@mb_convert_encoding($address, 'GBK', 'UTF-8').'",';
echo'"'.@mb_convert_encoding($board_total, 'GBK', 'UTF-8').'",';
echo'"'.@mb_convert_encoding($first_login_time, 'GBK', 'UTF-8').'",';
echo'"'.@mb_convert_encoding($goods_count, 'GBK', 'UTF-8').'",';
echo'"'.@mb_convert_encoding($pay_sum, 'GBK', 'UTF-8').'",';
echo"\r\n";
}
}
}