【问题标题】:Call to undefined method Maatwebsite\\Excel\\Excel::selectSheetsByIndex()调用未定义的方法 Maatwebsite\\Excel\\Excel::selectSheetsByIndex()
【发布时间】:2021-02-06 03:26:37
【问题描述】:

我正在将 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 中编写。

【问题讨论】:

标签: php excel laravel backend maatwebsite-excel


【解决方案1】:

调试了很多,终于找到了答案。

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

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;
}
}

如果您在收集函数中返回任何内容,则不会将其发送回控制器。因此,如果要将数据发送到控制器,则需要创建一个公共变量,然后在控制器中调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-02
    • 2018-10-28
    • 2018-11-09
    • 2019-10-15
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多