码迷,mamicode.com
首页 > 其他好文 > 详细

导出大量数据到excel表

时间:2015-04-18 20:30:47      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

set_time_limit(0);
error_reporting(0);

// 输出Excel文件头,可把user.csv换成你要的文件名
header(‘Content-Type: application/vnd.ms-excel‘);
header(‘Content-Disposition: attachment;filename="user.csv"‘);
header(‘Cache-Control: max-age=0‘);

$fp = fopen(‘/tmp/export/data.csv‘, ‘a‘);
$head = array(‘UID‘, ‘EMAIL‘, ‘注册时间‘, ‘第一次登陆时间‘);
foreach ($head as $i => $v) {
// CSV的Excel支持GBK编码,一定要转换,否则乱码
$head[$i] = iconv(‘utf-8‘, ‘gbk‘, $v);
}
fputcsv($fp, $head);

$result = array(.................................);

foreach($resutl as $val){
ob_flush();
flush();

// 从数据库中获取数据,为了节省内存,不要把数据一次性读到内存,从句柄中一行一行读即可
$sql = "SELECT uid,email,regdate,lastlogintime FROM reg_members WHERE email in ($val) ORDER BY uid ASC, regdate ASC";
$query = $db_reg_read -> query($sql);
while ($res = $db_reg_read -> fetch_array($query)) {
$email = strtolower($res[‘email‘]);
$list[] = $res;
}

//对数据进行整理

//输入到CSV中
foreach($list as $key => $val){
$row[0] = iconv(‘utf-8‘, ‘gbk‘, $val[‘uid‘]);
$row[1] = iconv(‘utf-8‘, ‘gbk‘, $val[‘email‘]);
$row[2] = iconv(‘utf-8‘, ‘gbk‘, $val[‘regdate‘]);
$row[3] = iconv(‘utf-8‘, ‘gbk‘, $val[‘lastlogintime‘]);
fputcsv($fp, $row);
}
}

导出大量数据到excel表

标签:

原文地址:http://www.cnblogs.com/gide/p/4437950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!