传递给Maatwebsite \ Excel \ Excel :: download()的参数2必须为字符串类型,给定对象 [英] Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given

查看:175
本文介绍了传递给Maatwebsite \ Excel \ Excel :: download()的参数2必须为字符串类型,给定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传递给 Maatwebsite\Excel\Excel::download() 的参数 2 必须是类型字符串,给定的对象,在C:\ xampp \ htdocs \ student_route \ vendor \ laravel \ framework \ src \ Illuminate \ Support \ Facades \ Facade.php在第237行

Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given, called in C:\xampp\htdocs\student_route\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 237

public function excel_report()
    {
     $student_data = DB::table('student_details')->get()->toArray();
     $student_array[] = array('Name', 'Address', 'Roll No', 'Class');
     foreach($student_data as $student)
     {
      $student_array[] = array(
       'Student Name'  => $student->st_name,
       'Address'   => $student->address,
       'Roll No'   => $student->roll_no,
       'Class'    => $student->st_class
      );
     }
     Excel::download('Student_Data', function($excel) use ($student_array){
      $excel->setTitle('Student Datas');
      $excel->sheet('Student_Datass', function($sheet) use ($student_array){
       $sheet->fromArray($student_array, null, 'A1', false, false);
      });
     })->download('xlsx');
    }

我将错误参数2传递给Maatwebsite \ Excel \ Excel :: download()必须是字符串类型,给定的对象.不知道问题出在哪里.任何人都请检查.我正在使用laravel 5.8

I got error Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given.don't know where is issue. anyone check please. i am using laravel 5.8

推荐答案

我没有使用该软件包,但是您可以从

I haven't use that package, but you can see from the method signature that the second argument expects a string that will be the name of the file to download:

public function download($export, string $fileName, string $writerType = null, array $headers = [])

在您的情况下,您正在返回回调.

In your case, you are returning a callback.

更新1

看到文档,我认为您正在尝试使用 Excel 类的静态 create 方法,但是您使用的是(错误地)下载一个.从 文档:

Seeing the docs, I think that you what you are trying to use the static create method of the Excel class, but instead you are using (incorrectly) the download one. From the docs:

从数组创建工作表

数组

要从数组创建新文件,请使用 ->fromArray($source,$ nullValue,$ startCell,$ strictNullComparison,$ headingGeneration)在表 closures 中.

Excel::create('Filename', function($excel) {

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

        $sheet->fromArray(array(
            array('data1', 'data2'),
            array('data3', 'data4')
        ));

    });

})->export('xls');

因此,在您的情况下,将第一个 download](...)替换为 create(...):

public function excel_report()
{
    $student_data = DB::table('student_details')->get()->toArray();
    $student_array[] = array('Name', 'Address', 'Roll No', 'Class');

    foreach ($student_data as $student)
    {
        $student_array[] = array(
            'Student Name' => $student->st_name,
            'Address'      => $student->address,
            'Roll No'      => $student->roll_no,
            'Class'        => $student->st_class
        );
    }

    Excel::create('Student_Data', function ($excel) use ($student_array) {
//        ^^^^^^^
        $excel->setTitle('Student Datas');
        $excel->sheet('Student_Datass', function ($sheet) use ($student_array) {
            $sheet->fromArray($student_array, null, 'A1', false, false);
        });
    })->export('xls');
//    ^^^^^^^^^^^^^^
}


更新2

上面的代码适用于 v2 ,但是鉴于您使用的是 v3 ,这无济于事.在当前版本中,您可以结合使用 Export s和 Sheet 类格式化输出文件.实际上,这是一种更加模块化的方法,可以解耦和改进您的代码.检查文档的本节.


Update 2

The above code works for v2, but given that you are using the v3 this can't help you. In the current version, you can format your output file with a combination of Exports and Sheet classes. Actually, this is more modular approach to decouple and improve your code. Check this section of the docs.

这篇关于传递给Maatwebsite \ Excel \ Excel :: download()的参数2必须为字符串类型,给定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆