PHP 实时生成并下载超大数据量的 Excel 文件

    //另外由于excel数据是从数据库里逐步读出然后写入输出流的所以需要将PHP的执行时间设长一点
    //(默认30秒)set_time_limit(0)不对PHP执行时间做限制。
    set_time_limit(0);
    $columns = [
       '文章ID', '文章标题', ...... //'openid'
    ];
    //处理需要导出的数据
    $timeStart = strtotime('2018-08-08 00:00:00');
    $timeEnd = strtotime('2018-08-08 23:59:59');
    $arr = DB::table('t_wechat_user_wx4ed9e1f4e0f3eeb0')->select(DB::raw('distinct openid'))->where('subscribe_time','>=',$timeStart)->where('subscribe_time','<=',$timeEnd);

    $csvFileName = '8月8号新增粉丝openid.xlsx';
    //设置好告诉浏览器要下载excel文件的headers
    header('Content-Description: File Transfer');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="'. $csvFileName .'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    $fp = fopen('php://output', 'a');//打开output流
    mb_convert_variables('GBK', 'UTF-8', $columns);
    fputcsv($fp, $columns);//将数据格式化为CSV格式并写入到output流中
    $accessNum = $arr->count();//从数据库获取总量,假设是一百万
    $perSize = 1000;//每次查询的条数
    $pages   = ceil($accessNum / $perSize);
    $lastId  = 0;
    for($i = 1; $i <= $pages; $i++) {
        //需要导出的数据
        $accessLog = $arr->offset($lastId)->limit($perSize)->get(['openid'])->toArray();
        foreach($accessLog as $access) {
            $rowData = [
                ......//每一行的数据  $access->openid
            ];
            mb_convert_variables('GBK', 'UTF-8', $rowData);
            fputcsv($fp, $rowData);
            $lastId ++;
        }
        unset($accessLog);//释放变量的内存
        //刷新输出缓冲到浏览器
        ob_flush();
        flush();//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
    }
    fclose($fp);
    exit();
本作品采用《CC 协议》,转载必须注明作者和本文链接
不要轻易放弃。学习成长的路上,我们长路漫漫,只因学无止境 Don't give up easily. On the way of learning and growing up, we have a long way to go, just because there is no end to learning.
本帖由系统于 4年前 自动加精
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 5

这就是 csv 为什么还有指定拓展名为 xlsx

5年前 评论

记得有个 chunk 查询 了解一下..

5年前 评论

id如果不连续有问题吧

for($i = 1; $i <= $pages; $i++) {
       $skip = $i*$perSize ;//
        //需要导出的数据
        $accessLog = $arr->offset($skip)->limit($perSize)->get(['openid'])->toArray();
        foreach($accessLog as $access) {
            $rowData = [
                ......//每一行的数据  $access->openid
            ];
            mb_convert_variables('GBK', 'UTF-8', $rowData);
            fputcsv($fp, $rowData);
           // $lastId ++;
        }
        unset($accessLog);//释放变量的内存
        //刷新输出缓冲到浏览器
        ob_flush();
        flush();//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
    }
5年前 评论
xujun0429

这个是导出csv的吧?能直接导出xlsx?

4年前 评论
xuri

推荐一个功能丰富、兼容性好、高性能的 Excel 文档基础库:github.com/xuri/excelize

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!