原理很简单,就是用php输出一个header头,可以直接让浏览器下载文件
单元格内容用英文逗号分隔, 其中为了防止乱码,汉字部分会进行一个转码操作,从utf8转为gbk
为了使单元格看起来直观一点,会增加个t ,这样就不会看的时候出现#########
比如下面这个把moveusers数组作为csv文件下载
代码语言:javascript复制if(isset($_REQUEST['act'])&& $_REQUEST['act']=="downloadAll") {
$outformat = "entmove.csv";
$title = "邮箱名,姓名,邮箱密码,搬家状态,开始时间,结束时间,邮件总数,收取数,失败数n";
$data = "";
foreach ($moveUsers as $b) {
$status = iconv("utf-8", "gbk//IGNORE", $b['status_text']);
$realName = iconv("utf-8", "gbk//IGNORE", $b['realName']);
$data .= "{$b['account']}t,{$realName}t,{$b['password']},{$status}t,{$b['movetime']}t"
. ",{$b['endtime']}t,{$b['totle']},{$b['successnum']},{$b['failnum']}n";
}
$title = iconv("utf-8", "gbk//IGNORE", $title);
$data = $title . $data;
header("Cache-control: private");
header("Pragma: public");
header('Content-type: application/x-csv');
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5')) {
header("Content-Disposition: inline; filename=$outformat");
} else {
header("Content-Disposition: attachment; filename=$outformat");
}
echo $data;
exit;
}