PHP Excel的导入与导出功能

 为什么做这件事:也许原因大家都各不相同,可能有的是为了工作,可能有的是为了学习,我的原因很简单,就是为了玩,正应为好玩,才没事的时候搞一搞。

这件事情很简单,你需要引入一个github库

maatwebsite/excel


然后http://www.maatwebsite.nl/laravel-excel/docs 这篇文章就可以很好的对import,export and export from view files很好的运用了

Let‘s’ begin


核心功能:导入到数据库功能

   /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
        /**
     * @description 通过excel表格导入用户数据,帐号和密码都是邮箱号
    */
    public function index(){
        Excel::load(base_path(). '/storage/app/public/file.xlsx', function($reader) {
            $data = $reader->toArray();


            foreach($data[0] as $key => $v){
                    $userinfo = User::where('email',$v['email'])->first();
                    if(is_null($userinfo)){
                        $password = $v['email'];
                        $name = $v['name'];
                        $user_id = User::insertGetId([
                            'name'=>$name,
                            'email' => $v['email'],
                            'password' => bcrypt($password),
                            'created_at' => date('Y-m-d H:i:s',time()),
                        ]);
                    }else{
                        $user_id = $userinfo->id;
                    }
                    echo $user_id."<br>";
                }
        });
    }

如果你需要导出pdf那么你还另需要引入一下这些文件的其中一个,里边她会悄悄告诉怎么做的,那么这个是你们两个的悄悄话了,我就不便谈及了。

Export to PDF

To export files to pdf, you will have to include "dompdf/dompdf": "~0.6.1""mpdf/mpdf": "~5.7.3" or "tecnick.com/tcpdf": "~6.0.0" in your composer.json and change the export.pdf.driver config setting accordingly.


  'pdf'                         => [
            /*
            |--------------------------------------------------------------------------
            | PDF Drivers
            |--------------------------------------------------------------------------
            | Supported: DomPDF, tcPDF, mPDF
            */
           
'driver'  => 'mPDF', (重点,composer 拉取的那个就用哪一个默认是DomPDF,我用的是mPDF)


            /*
            |--------------------------------------------------------------------------
            | PDF Driver settings
            |--------------------------------------------------------------------------
            */
            'drivers' => [


                /*
                |--------------------------------------------------------------------------
                | DomPDF settings
                |--------------------------------------------------------------------------
                */
                'DomPDF' => [
                    'path' => base_path('vendor/dompdf/dompdf/')
                ],


                /*
                |--------------------------------------------------------------------------
                | tcPDF settings
                |--------------------------------------------------------------------------
                */
                'tcPDF'  => [
                    'path' => base_path('vendor/tecnick.com/tcpdf/')
                ],


                /*
                |--------------------------------------------------------------------------
                | mPDF settings
                |--------------------------------------------------------------------------
                */
                'mPDF'   => [
                    'path' => base_path('vendor/mpdf/mpdf/')
                ],
            ]
        ]
    ],


从数据库导出功能

  public function export(){



        $data = User::orderBy('id','desc')->get()->toArray();


        Excel::create('用户信息表',function($excel) use($data){


        $excel->sheet('Sheetname', function($sheet) use($data) {


            $sheet->fromArray($data);


        });


        })->export('xls');

       // })->export('pdf'); 上边如果用的是pdf这个地方需要修改pdf

    }


导出的excel文件:


导出的pdf文件:



存储在服务器上

要将创建的文件存储在服务器上,请使用->store($ext, $path = false, $returnInfo = false)->save()

正常导出到默认存储路径

默认情况下,该文件将存储在app/storage/exports文件夹中,该文件夹已在export.php配置文件中定义

如果你是laravel5以上版本,想要上传到xls文件到服务器上,你需要使用命令copy一份excel.php文件到你的config目录下:

php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
This will add an  excel.php  config file to your config folder.


就这么多了,遇到不懂的问题,共同讨论!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值