导出40万条+数据没问题 (使用php命令行执行)
直接弹出下载文件
// 输出到浏览器
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userlog.csv"');
header('Cache-Control: max-age=0');
$fp = fopen('php://output', 'a');
// title
$title = array('商户编号', '商户用户名', '动作', '操作内容', 'ip', '操作时间');
// 转码
foreach ($title as $key => $value ) {
$head[$key] = iconv('UTF-8', 'GBK', $value);
}
fputcsv($fp, $title);
error_reporting(0);
ini_set('memory_limit', '128M');
set_time_limit(0);
$db = new \db\RDB();
$wherestr ="1=1 ";
$count = $db->get_one('select count(1) as total from `pay_user_log` where '.$wherestr);
$count = $count['total'];
// 每次写入5000条
$rowsE = 5000;
$pagesE = ($count%$rowsE == 0) ? $count/$rowsE : (int)($count/$rowsE) + 1;
for ($i = 0; $i < $pagesE; $i++)
{
// 查询数据
$data = $db->get_all('select * from `pay_user_log` where ' . $wherestr . ' order by `addtime` desc limit '.($i * $rowsE).', '.$rowsE);
// 定义要输出的数据
for ($j = 0; $j < count($data); $j++)
{
$formatData = array();
$formatData = array(
$data[$j]['UserID'],
$data[$j]['UserName'],
$data[$j]['action'],
$data[$j]['remark'],
$data[$j]['ip'],
'\''.date('Y-m-d H:i:s', $data[$j]['addtime'])
);
fputcsv($fp, $formatData);
unset($formatData);
}
unset($data);
}
2. 生成csv到指定目录下
$filename = 'upload/' . Help::randStr(10) . '.csv';
$fp = fopen(PUBLIC_PATH . $filename, 'a');
// title
$title = array('序号', '兑换码编号', '兑换码内容', '兑换人昵称', '兑换人盯盯号', '兑换人手机号', '兑换时间', '兑换码状态', '所属渠道', '有效期');
// 转码
foreach ($title as $key => $value) {
$head[$key] = iconv('UTF-8', 'GBK', $value);
}
fputcsv($fp, $title);
//ini_set('memory_limit', '64M');
set_time_limit(0);
// 查询数据
$list = $this->db->select('coupon_code', '*', ['id' => $idarr]);
$time = time();
// 定义要输出的数据
$i = 1;
foreach ($list as $key => $val) {
$coupon = $this->db->get('coupon', ['[>]coupon_qudao' => ['qid' => 'id']], ['coupon_qudao.title', 'coupon.time_start', 'coupon.time_end'], ['coupon.id' => $list[$key]['pid']]);
$formatData = array(
$i,
'> ' . $list[$key]['bianhao'],
'> ' . $list[$key]['code'],
$list[$key]['user_nickname'],
$list[$key]['user_dingding'],
$list[$key]['user_mobile'],
$lasttime,
$status_text,
$coupon['title'],
date('Y-m-d H:i:s', $coupon['time_start']) . ' 至 ' . date('Y-m-d H:i:s', $coupon['time_end']),
);
fputcsv($fp, $formatData);
unset($formatData);
$i++;
}
unset($data);
Help::sys_out_success('', $filename);