导出csv

导出方法

  /**
     * 导出csv
     *
     * @param String $sFileName
     * @param array $aTitle
     * @param $oQuery
     * @param Closure $closure
     * @author zjh
     */
    public function csv(String $sFileName,Array $aTitle, $oQuery, Closure $closure)
    {
        // 设置过期时间
        set_time_limit(0);
        //处理需要导出的数据
        //设置好告诉浏览器要下载excel文件的headers
        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="'. $sFileName .'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        $fp = fopen('php://output', 'a');//打开output流
        fwrite($fp, "\xEF\xBB\xBF"); // 写入bom 头 可识别 utf8
        fputcsv($fp, $aTitle);//将数据格式化为CSV格式并写入到output流中
        $accessNum = $oQuery->count();//从数据库获取总量,假设是一百万
        $perSize = 10000;//每次查询的条数
        $pages   = ceil($accessNum / $perSize);
        for($i = 1; $i <= $pages; $i++) {
            //需要导出的数据
            $oCollection = $oQuery->forPage($i,$perSize)->get();
            foreach($oCollection as $obj) {
                $rowData = $closure($obj); //返回 array
                fputcsv($fp, $rowData);
            }
            unset($oCollection);//释放变量的内存
            //刷新输出缓冲到浏览器
            if (ob_get_level() > 0) {
                ob_flush();
            }
        }
        fclose($fp);
        exit();
    }

    /**
 * 通过数组导出
  * @param $sFileName
  * @param array $aTitle
  * @param $aData
  * @param $closure
  */
public function exportCsvByArray($sFileName,Array $aTitle, $aData)
{
  // 设置过期时间
  set_time_limit(0);
  //处理需要导出的数据
  //设置好告诉浏览器要下载excel文件的headers
  header('Content-Description: File Transfer');
  header('Content-Type: application/vnd.ms-excel');
  header('Content-Disposition: attachment; filename="'. $sFileName .'"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  $fp = fopen('php://output', 'a');//打开output流
  fwrite($fp, "\xEF\xBB\xBF"); // 写入 bom 头
  fputcsv($fp, $aTitle);//将数据格式化为CSV格式并写入到output流中
  foreach($aData as $rowData) {
  fputcsv($fp, $rowData);
  }
  fclose($fp);
  exit();
}

使用

   // 定义表头
        $aTitle=[
            '编号','用户姓名','邮箱','创建时间'
        ];
        // 导出文件名
        $csvFileName = '用户数据统计.csv';
        // 数据源 使用DB::table方式更高效
        $oQuery = DB::table("users")->select('id','name','email','created_at');
        // 数据加工
        $this->csv($csvFileName,$aTitle,$oQuery,function ($obj){
            return [
                $obj->id , $obj->name ,$obj->email, $obj->created_at
            ];
        });

注意事项

  • 表头Tile不要以英文字母开头,否则打开会有文件损坏的不友好提示
本作品采用《CC 协议》,转载必须注明作者和本文链接
喜欢的话就点个赞吧!
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 4
sanders

用chunk方法分页简单一些

3年前 评论

若需要使用chunk,建议使用 chuunkById 支持版本laravel 5.5以上

DB::table('users')->where('active', false)
    ->chunkById(100, function ($users) {
        foreach ($users as $user) {
            DB::table('users')
                ->where('id', $user->id)
                ->update(['active' => true]);
        }
    });
3年前 评论

www.cnblogs.com/-mrl/p/7686640.htm... 使用html 文件流导出excel 可以合并 设置样式

3年前 评论

没有文件下载, 在network上看到的是 原文输出

1年前 评论

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