zoukankan      html  css  js  c++  java
  • 使用 Laravel-Excel 和流的方法导出 Excel

    1、使用 laravel-excel 扩展包导出

    扩展包的 3.0 的版本和 2.0 相比做了很多的改动,个人感觉更容易使用了。扩展包给出了很多基于 query 的导出,视图的导出。下面例子为基于 array 的导出,其他的查看文档即可。

    导出类:

    use MaatwebsiteExcelConcernsFromCollection;
        use MaatwebsiteExcelConcernsExportable;
        use MaatwebsiteExcelConcernsWithHeadings;
        class UsersExport implements FromCollection, WithHeadings
        {
            use Exportable;
    
            private $data;
            private $headings;
    
            //数据注入
            public function __construct($data, $headings)
            {
                $this->data = $data;
                $this->headings = $headings;
            }
    
            //实现FromCollection接口
            public function collection()
            {
                return collect($this->data);
            }
    
            //实现WithHeadings接口
            public function headings(): array
            {
                return $this->headings;
            }
    
        }

    控制器中导出

    namespace AppHttpControllers;
    
        use MaatwebsiteExcelFacadesExcel;
        use AppExportsUsersExport;
        class UsersController extends Controller
        {
            public function export()
            {
                $data = [
                    [
                        'name' => 'cheng',
                        'email' => 'cheng111'
                    ],
                    [
                        'name' => 'cheng',
                        'email' => 'cheng111'
                    ],
                ];
    
                $headings = [
                    'name',
                    'email'
                ];
                return Excel::download(new UsersExport($data, $headings), 'users.csv');
    
            }
        }

    2、使用流的形式导出

     public function export($params)
            {
                set_time_limit(0);
    
                $columns = ['字段名'];
    
                $fileName = 'GPS管理明细' . '.csv';
                //设置好告诉浏览器要下载excel文件的headers
                header('Content-Description: File Transfer');
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment; filename="'. $fileName .'"');
                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流中
    
                $num = $this->getExportNum($params);
                $perSize = 2000;//每次查询的条数
                $pages   = ceil($num / $perSize);
    
                for($i = 1; $i <= $pages; $i++) {
                    $list = $this->getUnitExportData($params, $i, $perSize);
    
                    foreach($list as $item) {
                        $rowData[] = $this->switchDeviceMakerToDesc($item['device_maker']);
                        .
                        .
                        .
                        mb_convert_variables('GBK', 'UTF-8', $rowData);
                        fputcsv($fp, $rowData);
                        unset($rowData);
                    }
                    unset($accessLog);//释放变量的内存
    
                    ob_flush();     //刷新输出缓冲到浏览器
                    flush();        //必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
                }
                fclose($fp);
                exit();
            }

     原文:https://learnku.com/articles/17829

  • 相关阅读:
    【Java基础】2、Java中普通代码块,构造代码块,静态代码块区别及代码示例
    【Java基础】1、java中的instanceof
    【Java面试】1、基础知识篇
    【github&&git】3、git图像化界面GUI的使用
    【github&&git】2、github入门到上传本地项目
    Spring Boot属性配置文件详解
    微服务Spring Cloud与Kubernetes比较
    导入时如何定制spring-boot依赖项的版本
    spring容器ApplicationContext初始化(spring应用上下文初始化)
    spring概念简介、bean扫描与注册实现方式
  • 原文地址:https://www.cnblogs.com/niuben/p/11458450.html
Copyright © 2011-2022 走看看