thinkphp3.2数据导出,亲测导出8W条数据没有任何问题 /** * 订单导出 * @param $where */ function export($where) { $f_name = "全部订单"; //列名称 $title = array('序号', '奖品名称', '手机号', '验证状态',
/**
* 订单导出
* @param $where
*/
function export($where)
{
$f_name = "全部订单";
//列名称
$title = array('序号',
'奖品名称',
'手机号',
'验证状态',
'验证时间',
'抽奖时间'
);
$fileName = $f_name . date('_YmdHis');//文件名称
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
ob_flush();
flush();
$order = M("Order");
$f = "title,mobile,status,api_time,add_time";//尽量只取需要的字段,不要取出全部
$o = 'id desc';
//分页查
$total_count = M('order')->where($where)->count();
$num_page = 5000;
$page = ceil($total_count / $num_page);
//创建临时存储内存
$fp = fopen('php://memory', 'w');
//商品只有6个 所以全部取出
$order_type = M("Goods")->getField("type,title", true);
fputcsv($fp, $title, ',');
$i =1;
for ($j = 0; $j < $page; $j++) {
$num = $num_page * $j;
$limit = $num . ',' . $num_page;
$data = $order->field($f)->where($where)->order($o)->limit($limit)->select();
foreach ($data as $item) {
$title = $order_type[$item['title']];
$xlsData = array($i,
$title,
"\t" . $item['mobile'],
$item['status'] ? '已验证' : '未验证',
"\t" . $item['api_time'],
"\t" . $item['add_time']
);
fputcsv($fp, $xlsData, ',');
$i++;
}
}
rewind($fp);
$content = "";
while (!feof($fp)) {
$content .= fread($fp, 1024);
}
fclose($fp);
$content = iconv('utf-8', 'gbk', $content);//转成gbk,否则excel打开乱码
echo $content;
exit;
}
