美文网首页PHP经验分享PHP实战
php实现导出10万条数据

php实现导出10万条数据

作者: 日落之国 | 来源:发表于2019-02-14 11:02 被阅读4次

开发中经常遇到需要从数据库导出大量数据的问题,导出excel需要占用太多内存,最终回导致内存溢出而失败。csv是更好的选择。同时可以使用php5.5之后赋予的新功能——yield(生成器)来优化性能,具体可以看鸟哥博客http://www.laruence.com/tag/yield

/**
     * 分段导出所有用户
     */
    public function exportAll()
    {
        set_time_limit(0);
        $count = D('user')->count();
        $num = 0;
        $f = null;
        foreach ($this->getCounts($count,function($fp) use (&$f){
            $f = $fp;
        }) as $value) {
            $num++;
            if (1000 === $num) {
                ob_flush();
                flush();
                $num = 0;
            }
            foreach ($value as $v) {
                fputcsv($f,$v);
            }
            
            fclose($f);
        }
        //导出合并后的文件
        $this->mergeFile(function($file){
            error_reporting(0);
            header("Cache-Control: max-age=0");
            header("Content-Description: File Transfer");
            header('Content-disposition: attachment; filename=songdaozong.csv'); // 文件名
            header("Content-Type: application/csv"); 
            header("Content-Transfer-Encoding: binary"); 
            header('Content-Length: ' . filesize($file));
            readfile($file);//输出文件;
            self::clearFile();
        });
    }
/**
     * 获取阶段导出数据
     */
    private function getCounts($count,$handle)
    {
        $pageCount = ceil($count/1000);
        for ($i=0; $i < $pageCount; $i++) { 
            $file = __DIR__ . '/_' . $i . '.csv';
            touch($file);
            $fp = fopen($file, 'w'); //生成临时文件
            if ($handle instanceof \closure) {
                $handle($fp);
            }
            yield D('user')->limit($i*1000,1000)->select();
        }
    }

    /** 
     * 处理数组格式
     */
    private static function makeArray(array $data)
    {
        $new = [];
        foreach ($data as $key => $value) {
            $new[] = array_values($value);
        }

        return $new;
    }

    /**
     * 合并文件
     */
    private function mergeFile($handle)
    {
        $fileList = glob(__DIR__.'/*.csv');
        $count = count($fileList);
        for ($i=0; $i < $count; $i++) { 
            if ($i > 0) {
                file_put_contents($fileList[0],file_get_contents($fileList[$i]),FILE_APPEND);
            }
        }
        if ($handle instanceof \closure) {
            $handle($fileList[0]);
        }
    }

    /** 
     * 清除文件
     */
    private static function clearFile()
    {
        error_reporting(0);
        $fileList = glob(__DIR__.'/*.csv');
        foreach ($fileList as $value) {
            unlink($value);
        }
    }

相关文章

  • php实现导出10万条数据

    开发中经常遇到需要从数据库导出大量数据的问题,导出excel需要占用太多内存,最终回导致内存溢出而失败。csv是更...

  • PHPExcel数据导入

    PHPExcel是一个PHP类库,用来帮助我们简单、高效实现从Excel读取Excel的数据和导出数据到Excel...

  • phpexcle大数据量导出

    phpexcle大数据量导出 PHP处理大数据导出Excel方法 RunningWin_4c80 字数 298 ·...

  • Java菜谱(四)——怎么将10万条数据导出到excel?

    今天的菜品是这样的: 怎么将10万条数据导出到excel? 太长不看版 xls格式最多可以存65536行数据,而x...

  • excel 导出

  • php 数据导出

    最近在做后台管理的项目,后台通常有数据导出到 excel 的需要,经过之前搜索通常推荐使用的是 php excel...

  • PHP创建的csv文件在windows上乱码的问题

    前言 做PHP开发的时候,由于业务需要,可能要经常导出csv文件。使用PHP将数据导出到csv文件不难,使用fpu...

  • 性能优化:大表数据导出

    最近项目中有一个需求:根据查询条件导出(mysql)单表数据(几千万条数据量级的导出)。而查询条件各种各样,无法在...

  • Poi实现保护工作表后可新增与删除行

    背景 最近项目需要实现数据的导出,并支持导入数据实现新增或更新数据 需求 隐藏列:因为需要更新数据,导出的exce...

  • 数据差异运算

    又来吐槽一下码狗的办公日常,被要求从数据库批量导出数据,发现源数据条目与导出的目标数据不一致,在上万条数据中寻找几...

网友评论

    本文标题:php实现导出10万条数据

    本文链接:https://www.haomeiwen.com/subject/etgseqtx.html