PHP导出CSV文件

释放双眼,带上耳机,听听看~!

导出40万条+数据没问题 (使用php命令行执行)

直接弹出下载文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
1// 输出到浏览器
2header('Content-Type: application/vnd.ms-excel');
3header('Content-Disposition: attachment;filename="userlog.csv"');
4header('Cache-Control: max-age=0');
5$fp = fopen('php://output', 'a');
6// title
7$title = array('商户编号', '商户用户名', '动作', '操作内容', 'ip', '操作时间');
8// 转码
9foreach ($title as $key => $value ) {
10$head[$key] = iconv('UTF-8', 'GBK', $value);
11}
12fputcsv($fp, $title);
13error_reporting(0);
14ini_set('memory_limit', '128M');
15set_time_limit(0);
16$db = new \db\RDB();
17$wherestr ="1=1 ";
18
19$count = $db->get_one('select count(1) as total from `pay_user_log` where '.$wherestr);
20$count = $count['total'];   
21// 每次写入5000条
22$rowsE = 5000;
23$pagesE = ($count%$rowsE == 0) ? $count/$rowsE : (int)($count/$rowsE) + 1;
24for ($i = 0; $i < $pagesE; $i++) 
25{
26    // 查询数据         
27    $data = $db->get_all('select * from `pay_user_log` where ' . $wherestr . ' order by `addtime` desc limit '.($i * $rowsE).', '.$rowsE);
28    // 定义要输出的数据
29    for ($j = 0; $j < count($data); $j++) 
30    {
31        $formatData = array();
32        $formatData = array(
33            $data[$j]['UserID'],
34            $data[$j]['UserName'],
35            $data[$j]['action'],
36            $data[$j]['remark'],
37            $data[$j]['ip'],
38            '\''.date('Y-m-d H:i:s', $data[$j]['addtime'])
39        );
40        fputcsv($fp, $formatData);
41        unset($formatData);
42    }
43    unset($data);
44}
45
  1. 生成csv到指定目录下


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
1$filename = 'upload/' . Help::randStr(10) . '.csv';
2$fp = fopen(PUBLIC_PATH . $filename, 'a');
3// title
4$title = array('序号', '兑换码编号', '兑换码内容', '兑换人昵称', '兑换人盯盯号', '兑换人手机号', '兑换时间', '兑换码状态', '所属渠道', '有效期');
5// 转码
6foreach ($title as $key => $value) {
7    $head[$key] = iconv('UTF-8', 'GBK', $value);
8}
9fputcsv($fp, $title);
10//ini_set('memory_limit', '64M');
11set_time_limit(0);
12// 查询数据
13$list = $this->db->select('coupon_code', '*', ['id' => $idarr]);
14$time = time();
15
16// 定义要输出的数据
17$i = 1;
18foreach ($list as $key => $val) {
19    $coupon = $this->db->get('coupon', ['[>]coupon_qudao' => ['qid' => 'id']], ['coupon_qudao.title', 'coupon.time_start', 'coupon.time_end'], ['coupon.id' => $list[$key]['pid']]);
20    $formatData = array(
21        $i,
22        '> ' . $list[$key]['bianhao'],
23        '> ' . $list[$key]['code'],
24        $list[$key]['user_nickname'],
25        $list[$key]['user_dingding'],
26        $list[$key]['user_mobile'],
27        $lasttime,
28        $status_text,
29        $coupon['title'],
30        date('Y-m-d H:i:s', $coupon['time_start']) . ' 至 ' . date('Y-m-d H:i:s', $coupon['time_end']),
31    );
32    fputcsv($fp, $formatData);
33    unset($formatData);
34    $i++;
35}
36unset($data);
37Help::sys_out_success('', $filename);
38

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++迭代器

2022-1-11 12:36:11

病毒疫情

福建省新型冠状病毒肺炎疫情情况

2020-3-25 8:53:00

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索