【问题标题】:export excel file maatwebsite laravel导出excel文件maatwebsite laravel
【发布时间】:2019-05-20 02:37:59
【问题描述】:

我阻止从 laravel 上的表中导出 excel 文件,这是我的控制器:

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Excel;

class ExportExcelController extends Controller
{
  function Export()
  {
    $customer_data = DB::table('qualys')->get();
    return view('export_excel')->with('customer_data', $customer_data);
  }
  function excel()
  {
    $customer_data = DB::table('qualys')->get();
    $customer_array[] = array('ip','qid');
    // dd($customer_data);
    foreach($customer_data as $customer)
    {
      // dd($customer);
       $customer_array[] = array(
                 'ip' => $customer->qid,
                 'qid' => $customer->ip
      );
    }


     Excel::download('customer data', function($excel) use ($customer_array) {
          $excel->setTitle('customer Data');
         $excel->sheet('Customer Data', function($sheet) use ($customer_array)
          {
            $sheet->fromArray($customer_array, null, 'A1', false, false);
          });
     })->download('xls', 'test');
  }
}

我在第 29 行被阻止,我无法取回文件,这是以下错误:

Argument 2 passed to Maatwebsite\Excel\Excel::download() must be of the type string, object given, called in /var/www/html/qualys/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 223

我已经测试了几个“代码”但没有任何效果:(你能帮我吗? 诚挚的。

【问题讨论】:

  • 下载函数的第一个参数需要是一个对象“download(object $export, string $fileName, string $writerType = null)”,你传递2个字符串
  • 没有注释,复制粘贴过程中的小错误,就是这样: Excel::download('customer data', function($excel) use ($customer_array) { $excel ->setTitle('客户数据'); $excel->sheet('客户数据', function($sheet) 使用 ($customer_array) { $sheet->fromArray($customer_array, null, 'A1', false, false ); }); })->下载('xls', 'test');

标签: php laravel laravel-5 frameworks


【解决方案1】:

你可以试试这段代码,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Excel;

class ExportExcelController extends Controller
{
  function Export()
  {
    $customer_data = DB::table('qualys')->get();
    return view('export_excel')->with('customer_data', $customer_data);
  }
  function excel()
  {
    $customer_data = DB::table('qualys')->get();
    $customer_array[] = array('ip','qid');
    // dd($customer_data);
    foreach($customer_data as $customer)
    {
      // dd($customer);
       $customer_array[] = array(
                 'ip' => $customer->qid,
                 'qid' => $customer->ip
      );
    }

      // change download to create
     Excel::create('filename', function($excel) use ($customer_array) {
          $excel->setTitle('customer Data');
         $excel->sheet('Customer Data', function($sheet) use ($customer_array)
          {
            $sheet->fromArray($customer_array, null, 'A1', false, false);
          });
     })->download('xls');
     // donwload method only accept one parament and that is file extension
  }
}

【讨论】:

  • 您好,从 maatwebsite/excel 3.1 版开始,“create”命令不再存在,取而代之的是 Excel::download/Excel::store($yourExport) 但不幸的是它不起作用.真诚的。
【解决方案2】:

没关系,我知道怎么用最新版本了-> 输入的第一个命令:

php artisan make:export UsersExport --model=User

然后创建模型你要导出的数据库

php artisan make:model User

然后是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Excel;
use App\Exports\QualysExport;
use App\Http\Controllers\Controller;

class ExportExcelController extends Controller
{
  function Export()
  {
    $customer_data = DB::table('user')->get();
    return view('export_excel')->with('customer_data', $customer_data);
  }

      public function excel()
      {
          return Excel::download(new UserExport, 'Users.xlsx');
      }

}

然后是下载文件的路径:

 Route::get('/excel_export/excel', 'ExportExcelController@excel')->name('export_excel.excel');

下载来了! 真诚的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 2021-12-29
    • 2019-05-15
    • 2020-05-03
    • 2020-09-04
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多