您的位置 首页 php

php导出超大csv文件(导出数据量建议大于1万)

//数据量在1万以下的建议使用phpexcel
<a target="_blank" href="{:url('api/%20export /index')}">导出</a>

//控制器里面的方法
static function getData($total_page=1, $page_nums=10000)
 {
 
 for($i=0; $i<$total_page; $i++)
 {
 //$sql = "SELECT * FROM xcx_members WHERE id > ({$i}*{$page_nums}) LIMIT {$page_nums}";
 $start = $i * $page_nums;
 $sql = "SELECT * FROM xcx_members LIMIT {$start},{$page_nums}";
 //$sql2="SELECT a.*, (select mobile from xcx_members b where b.id=a.parent_id) as tjr_mobile FROM xcx_members a where id>=(select id from xcx_members where v_type=1 order by id asc limit {$start},1) limit {$page_nums}";
 //此处members表需要组合索引 create index v_type_id_index on members(v_type,id);
 //字段v_type tinyint default 0
 $list = Db::query($sql)
 foreach($list as $k=>$v)
 {
 yield [$v['mobile'],$v['realname'],$v['nickname'],$v['avatar']];
 }
 
 unset($list);
 //每1万条数据就刷新缓冲区
 ob_flush();
 flush();
 //sleep(1);
 }
 }
 
 public function index()
 {
 //生成的 csv 文件名称
 $demo_csv_filename = time().".csv";
 
 //每页查询数量
 $page_nums = 10000;
 
 //总页数
 $count = Db::name("members")->count("id");
 if($count < $page_nums)
 {
 $total_page = 1;
 }
 else
 {
 $total_page = intval($count / $page_nums) + 1; 
 }
 //csv文件存入的路径
 $base_path = CMF_ROOT."public/upload/";
 
 //导入表头
 $title = [
 '手机号','真实姓名','昵称','头像'
 ];
 
 $fp = fopen($base_path.$demo_csv_filename, 'w');
 
 //写入标题
 fputcsv($fp, $title);
 
  fclose ($fp);
 
 $fp = fopen($base_path.$demo_csv_filename, 'a');
 
 foreach(self::getData($total_page, $page_nums) as $m=>$n)
 {
 //写入内容
 fputcsv($fp, $n);
 }
 
 fclose($fp);
 
 $download_url = cmf_get_domain()."/upload/".$demo_csv_filename;
 
 echo "< script  type='text/javascript'>document.location.href='{$download_url}'</script>";
 
 die;
 }

 

文章来源:智云一二三科技

文章标题:php导出超大csv文件(导出数据量建议大于1万)

文章地址:https://www.zhihuclub.com/154313.shtml

关于作者: 智云科技

热门文章

网站地图