首页 > 代码库 > CSV导出大量数据
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); }
CSV导出大量数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。