调用未定义的方法 Maatwebsite\\Excel\\Excel::selectSheetsByIndex()

Call to undefined method Maatwebsite\\Excel\\Excel::selectSheetsByIndex()

我正在 maatwebsite/excel 从 v2.0 升级到 v3.0。

public function getValidatedRows($uploadedFile)
{
    $validationRules = $this->getValidationRules();

    $sheet = Excel::selectSheetsByIndex(0)->load($uploadedFile);

    $rows = $sheet->all();

    if (!count($rows)) {
        throw new Exception('Invalid data.');
    }

    $parsedRows = [];

    foreach ($rows as $key => $row) {

        $rowNumber = $key + 2; // Added + 2 since key is zero-indexed and first row is for Headers.

        $columns = $row->toArray();

        $columns = array_slice($columns, 0, count($validationRules));

        if (count($columns) != count($validationRules)) {
            throw new Exception('Invalid data.');
        }

        $columnValues = array_values($columns);

        $columnKeys = array_keys($validationRules);

        $columnsWithValidKeys = array_combine($columnKeys ,$columnValues);

        $attributes = [
            'name'   => 'name',
            'email'  => 'email',
            'mobile' => 'mobile',
        ];

        $validationRowSuffix = "(row #{$rowNumber})";
        array_walk($attributes, function (&$item1, $key, $suffix) {
            $item1 = "$item1 $suffix";
        }, $validationRowSuffix);

        $attributes = array_filter($attributes);

        Validator::make($columnsWithValidKeys, $validationRules, [], $attributes)->validate();

        $parsedRows[] = $columnsWithValidKeys;
    }

    return $parsedRows;
}

我无法升级这段代码Excel::selectSheetsByIndex(0)->load($uploadedFile);

这是我升级后的导出文件此时的样子。

    <?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;

class GenericExport implements FromCollection, WithHeadings, WithStrictNullComparison
{
use Exportable;

private $heading, $data;

public function __construct($heading, $data)
{
    $this->heading = $heading;
    $this->data = $data;
}

public function collection()
{
    return collect($this->data);
}

public function headings(): array
{
    return $this->heading;
}

public function properties(): array
{
    return [
        'creator'        => 'xxxxxxxxxxx',
        'lastModifiedBy' => 'xxxxxxxxxxx',
        'title'          => 'Spreadsheet',
        'description'    => 'Default spreadsheet export',
        'subject'        => 'Spreadsheet export',
        'keywords'       => 'xxxxxxxx, excel',
        'category'       => 'Excel',
        'manager'        => 'xxxxxxxxx',
        'company'        => 'xxxxxxxxx',
    ];
}

}

我已经成功将其他所有内容升级到 v3.0。这里是 maatwebsite\excel.

documentation

上面的代码是函数出现问题的地方,下面的代码是我已经创建了一个导出文件并将其用于其他目的,例如下载,写入excel。

调试了很久终于找到答案了

我将把问题的解决方案放在下面:-

public function getValidatedRows($uploadedFile, $corporate)
{
     $validationRules = $this->getValidationRules();


    $employeeImport = new EmployeeImport;
    $sheet = Excel::import($employeeImport, $uploadedFile);
    $rows = $employeeImport->data;

    if (!count($rows)) {
        throw new Exception('Invalid data.');
    }

    $parsedRows = [];

    foreach ($rows as $key => $row) {

    $rowNumber = $key + 2; // Added + 2 since key is zero-indexed and first row is for Headers.

    $columns = $row;

    $columns = array_slice($columns, 0, count($validationRules));

    if (count($columns) != count($validationRules)) {
        throw new Exception('Invalid data.');
    }

    $columnValues = array_values($columns);

    $columnKeys = array_keys($validationRules);

    $columnsWithValidKeys = array_combine($columnKeys ,$columnValues);

    $attributes = [
        'name'   => 'name',
        'email'  => 'email',
        'mobile' => 'mobile',
    ];

    $validationRowSuffix = "(row #{$rowNumber})";
    array_walk($attributes, function (&$item1, $key, $suffix) {
        $item1 = "$item1 $suffix";
    }, $validationRowSuffix);

    $attributes = array_filter($attributes);

    Validator::make($columnsWithValidKeys, $validationRules, [], $attributes)->validate();

    $parsedRows[] = $columnsWithValidKeys;
}

    return $parsedRows;
}

通用导出文件将用于导出目的,而不是用于导入目的。导入需要制作导入文件

<?php

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class EmployeeImport implements ToCollection, WithHeadingRow {

public $data;

public function collection(Collection $rows) {

    $xyz = $rows->toArray();
    
    $this->data = $xyz;
    return $xyz;
}

public function headingRow(): int
{
    return 1;
}
}

如果您return收集函数中的任何内容,它都不会被发送回控制器。所以,如果你想将数据发送到控制器,你需要创建一个 public 变量,然后在控制器中调用它。