array('a','b','日本'), '1'=>array('a1','b1','中国'), '2'=>array('a2','b2','美国'), ); $title_arr=array('one','two','three'); exportCsv($data,$title_arr,'mytest'); function exportCsv($data,$title_arr,$file_name=''){ ini_set("max_execution_time", "3600"); $csv_data = ''; /** 标题 */ $nums = count($title_arr); for ($i = 0; $i < $nums - 1; ++$i) { $csv_data .= '"' . $title_arr[$i] . '",'; } if ($nums > 0) { $csv_data .= '"' . $title_arr[$nums - 1] . "\"\r\n"; } foreach ($data as $k => $row) { foreach ($row as $key => $r){ $row[$key] = str_replace("\"", "\"\"", $r); $csv_data .= "\"\t" . $row[$key] . '",'; } $csv_data .= '"' . "\"\r\n"; unset($data[$k]); } $csv_data = mb_convert_encoding($csv_data, "cp936", "UTF-8"); $file_name = empty($file_name) ? date('Y-m-d-H-i-s', time()) : $file_name; if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE")) { // 解决IE浏览器输出中文名乱码的bug $file_name = urlencode($file_name); $file_name = str_replace('+', '%20', $file_name); } $file_name = $file_name . '.csv'; header('Content-Type: application/download'); header("Content-type:text/csv;"); header("Content-Disposition:attachment;filename=" . $file_name); header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); header('Expires:0'); header('Pragma:public'); echo $csv_data; exit(); }
——————————————————————
$str = "店铺名,周期,销售量,收藏量,购物车量,访问量,取消量,评价量,好评量,中评量,差评量,返退量,销售额\n";
if(is_array($statics_list) && count($statics_list)>0)
{
foreach($statics_list as $k=>$v)
{
$str.=$v['store_name'].','.sumdate($v['sumdate']).','.$v['sales'].','.$v['collects'].','.$v['carts'].','.$v['visits'].','.$v['cancels'].','.$v['comments'].','.$v['goodcomments'].','.$v['normalcomments'].','.$v['badcomments'].','.$v['refunds'].','.$v['moneys']."\n";
}
}
$f_type=$this->_statics_mod->get_options_stype();
$str = iconv('utf-8','gb2312',$str);
$stype=isset($_GET['stype'])?intval($_GET['stype']):0;
$filename = date('Y-m-d').$f_type[$stype].'.csv'; //设置文件名
$this->_statics_mod->export_csv($filename,$str); //导出
function export_csv($filename,$data)
{
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $data;
}