• CSV导出大量数据


    复制代码
    $csvname = $csvname . '.csv';
    header('Content-Type: application/vnd.ms-excel;charset=GB2312');
    header('Content-Disposition: attachment;filename="' . $csvname . '"');
    header('Cache-Control: max-age=0');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-cache, must-revalidate'); //HTTP/1.1
    //open handle,php://output 表示直接输出到浏览器
    $fp = fopen('php://output', 'a');
    
    //output csv first column
    $head = $columName;
    foreach ($head as $i => $v) {
        $head[$i] = iconv('utf-8', 'gbk', $v);
    }
    fputcsv($fp, $head);
    
    // 计数器
    $cnt = 0;
    $limit = 2000;
    
    // 从数据库中获取数据,节省内存,从句柄中一行一行读即可
    $i = 2;
    
    foreach ($result as $key => $val) {
        $cnt++;
        //每隔$limit行,刷新一下输出buffer
        if ($limit == $cnt) {
            ob_flush();
            flush();
            $cnt = 0;
        }
    
        $rows[$i] = iconv('utf-8', 'gbk', $val['vendor_item']);
        $rows[$i + 1] = iconv('utf-8', 'gbk', $val['item_code']);
        $rows[$i + 2] = iconv('utf-8', 'gbk', $val['product_name']);
        $rows[$i + 3] = iconv('utf-8', 'gbk', $val['brand_code']);
        $rows[$i + 4] = iconv('utf-8', 'gbk', $val['brand']);
        $rows[$i + 5] = iconv('utf-8', 'gbk', $val['vendor_id']);
        $rows[$i + 6] = iconv('utf-8', 'gbk', $val['vendor_name']);
        $rows[$i + 7] = iconv('utf-8', 'gbk', $val['warehouse_code']);
        $rows[$i + 8] = iconv('utf-8', 'gbk', $val['quantity']);
        $rows[$i + 9] = iconv('utf-8', 'gbk', $val['lockcount']);
        $rows[$i + 10] = iconv('utf-8', 'gbk', $val['bindcount']);
        fputcsv($fp, $rows);
        //释放数组
        unset($rows);
    }
    复制代码
  • 相关阅读:
    webform 下使用autofac
    AutoFac (控制反转IOC 与依赖注入DI)
    AutoFac使用方法总结:Part I
    Asp.net三种事务处理
    这二十个问题,可能是你技术人生中已经或即将遭遇的痛点,怎么解?
    已经过事务处理的 MSMQ 绑定(转载)
    实践.Net Core在Linux环境下的第一个Hello World
    Linux新手扫盲(转载)
    Redis 数据类型及其特点
    观察者模式深入实践
  • 原文地址:https://www.cnblogs.com/zhangyuhang3/p/6873505.html
Copyright © 2020-2023  润新知